I want to see all the variables

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Fri Dec 29 11:35:01 EST 2006


On Fri, 29 Dec 2006 08:20:22 -0600, Larry Bates wrote:

> johnf wrote:
>> Hi,
>> When I use dir() I don't see the __ underscore items.  Is there anything
>> that will show all the private vars and functions?
>> 
>> johnf
> 
> The idea of the underscore items is that they aren't to be used by
> you.

Double leading+trailing underscore attributes are NOT private, they are
*special* but public (e.g. __dict__, __class__, __str__, etc.). If you
don't believe me, have a look at dir(int) and count the underscored
attributes listed. Then try to find __dict__, __name__, __bases__,
__base__ or __mro__ within the list. Why are they suppressed?

But even if underscored attributes were private, the Python philosophy is
that private attributes are private by convention only -- even
name-mangled __private methods can be reached if you know how.


> If you wish to access private variables and functions you will
> almost certainly have to look at the source code to make sure of
> what they are and how they can be utilized.

Not so.

>>> class Parrot(object):
...     def _private(self):
...             """Private method, returns a magic string."""
...             return "Don't touch!!!"
...
>>> Parrot._private.__doc__
"Private method, returns a magic string."



-- 
Steven.




More information about the Python-list mailing list