Python Forum
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
inivisible attribute
#1
i would like to create a global object in a class instance that is not visible as an instance attribute (e.g. private).  is this possible in python?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Could you show what you want with code? It sounds like the global keyword would accomplish that, just don't bind the variable to self.

That said, this sounds like a bad idea. Could you give more context?
Reply
#3
Wait, do you not actually want a global, but rather just an invisible instance attribute? Just use double underscore prefix. It's not bullet-proof but nothing in Python is.
Reply
#4
(Feb-10-2017, 06:02 PM)micseydel Wrote: Wait, do you not actually want a global, but rather just an invisible instance attribute? Just use double underscore prefix. It's not bullet-proof but nothing in Python is.

i want it to be visible to several methods within the class.  i guess i can just leave it visible outside the class and leave it up to the programmer using the class to not mess with it.  __using __a __double __underscore _on __its __name __sounds __like __a  __good __idea.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
Why would you consider leaving it "visible outside the class"? Why not double underscore?
Reply
#6
(Feb-12-2017, 01:48 AM)micseydel Wrote: Why would you consider leaving it "visible outside the class"? Why not double underscore?

if there is a way to make it invisible outside the class and i learn what that is then i will likely want to do that.  i really don't see a reason not to use double-underscore.  i just didn't think of that.  i was too tightly focused on finding a way to make invisible.  i think i read across that somewhere once.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
What does that mean? If you're thinking of private/protected in languages like C++/Java, Python doesn't have them. Python uses scope for managing "visibility" with the extra caveat that double underscores will create name mangling to external viewers.

So, in Python especially, don't think about "visibility" as much as scope.
Reply
#8
In Python is nothing private/protected.
Quote:we're all consenting adults here
The are many tutorial about _singel and __doble underscore that are wrong,
because people compare it to  C++/Java.

Can take a little about it.
_singel underscore tells user that this is internal attribute,can be changed but best to leave alone.
__doble underscore causes the name to be mangled to something else.
What's important with __doble is that attributes cannot be overridden via inheritance(because of name mangling).
Eg:
class A:
    _info = 'Do not change' # Internal class attribute
    def __init__(self):
        self.__key = 99  # private copy of this key

    def __foo(self):
        print('hello')

class B(A):
    def __init__(self):
        super().__init__()
        # Does not override A.__key
        self.__key = 100  

    # Does not override A.__foo_method()
    def __foo(self):
        print('world')
Use it,now we see how name mangling work with inheritance.
>>> obj = B()
>>> obj._A__key
99
>>> obj._B__key
100
so self.__key has same name in A and B,
but get  name mangling to different names for each class. 
The same with method:
>>> obj._A__foo()
hello
>>> obj._B__foo()
world

>>> # Class variable/attribute is shared with all classes
>>> obj._info
'Do not change'
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020