Feb-10-2017, 09:38 AM
Feb-10-2017, 06:01 PM
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?
That said, this sounds like a bad idea. Could you give more context?
Feb-10-2017, 06:02 PM
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.
Feb-11-2017, 01:43 AM
(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.
Feb-12-2017, 01:48 AM
Why would you consider leaving it "visible outside the class"? Why not double underscore?
Feb-12-2017, 05:22 AM
(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.
Feb-12-2017, 06:54 AM
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.
So, in Python especially, don't think about "visibility" as much as scope.
Feb-12-2017, 11:16 AM
In Python is nothing private/protected.
because people compare it to C++/Java.
Can take a little about it.
What's important with
Eg:
but get
The same with method:
Quote:we're all consenting adults hereThe 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 100so
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'