473,804 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I want to see all the variables

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
Dec 29 '06 #1
16 1673
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. 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.

-Larry
Dec 29 '06 #2
What do you mean? Can you specify which special functions you don't
see?
I get:
pyclass X:
pass
pydir(X)
['__doc__', '__module__']
pyclass X:
def __getitem__(sel f, x):
pass
pydir(X)
['__doc__', '__getitem__', '__module__']

On Dec 29, 12:35 pm, johnf <jfabi...@yolo. comwrote:
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
Dec 29 '06 #3
On Fri, 29 Dec 2006 07:57:30 -0800, wi******@hotmai l.com wrote:
What do you mean? Can you specify which special functions you don't
see?
I get:
pyclass X:
pass
pydir(X)
['__doc__', '__module__']
How about these?
>>X.__dict__
{'__module__': '__main__', '__doc__': None}
>>X.__name__
'X'
>>X.__bases__
()

Now that's interesting... if __name__ and __bases__ don't live in the
class __dict__, where do they live? What other methods and attributes are
invisibly in X?
--
Steven.

Dec 29 '06 #4
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+trailin g 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._priva te.__doc__
"Private method, returns a magic string."

--
Steven.

Dec 29 '06 #5


On Dec 29, 5:17 pm, Steven D'Aprano
<s...@REMOVE.TH IS.cybersource. com.auwrote:
On Fri, 29 Dec 2006 07:57:30 -0800, witte...@hotmai l.com wrote:
What do you mean? Can you specify which special functions you don't
see?
I get:
pyclass X:
pass
pydir(X)
['__doc__', '__module__']How about these?
>X.__dict__{'__ module__': '__main__', '__doc__': None}>>X.__name __
'X'
>X.__bases__( )

Now that's interesting... if __name__ and __bases__ don't live in the
class __dict__, where do they live? What other methods and attributes are
invisibly in X?

--
Steven.
Well, then we have to lookup what dir() does --
http://docs.python.org/lib/built-in-funcs.html -- it tries to bring up
the interesting attributes of an object, apparently __name__ and
__bases__ are considered to be not so interesting....

Dec 29 '06 #6
Steven D'Aprano wrote:
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+trailin g 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._priv ate.__doc__
"Private method, returns a magic string."
Ok then how do debug when I have something like "__source" and I need to
know what is available for the object?

John
Dec 29 '06 #7
On Fri, 29 Dec 2006 12:04:11 -0800, johnf wrote:
Ok then how do debug when I have something like "__source" and I need to
know what is available for the object?
Outside of a class, objects with two leading underscores are just ordinary
objects with no special behaviour:
>>__source = 2
__source
2

Objects with a single leading underscore like _source are slightly
special: if you do "from module import *" any names with a single leading
underscore are not imported.

Class attributes with two leading underscores like __source are considered
private to the class, so in general you shouldn't need to know anything
about them. To enforce that, Python mangles the name so it is harder to
reach.

But if/when you do need to get to them, they are easy to get to:
>>class Spam:
.... __attribute = 2
....
>>Spam().__attr ibute
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: Spam instance has no attribute '__attribute'
>>Spam()._Spam_ _attribute
2

Notice that Python doesn't generally try to hide "private" attributes:
>>dir(Spam)
['_Spam__attribu te', '__doc__', '__module__']

There are three other underscore conventions in use:

(1) Objects with a single leading underscore like _attribute are private
by convention, but Python doesn't enforce it. Starting an object with a
single underscore is like writing "# Private! Don't touch!" after it.

(2) By convention, if you want to create a name that is the same as a
built-in object without shadowing (hiding) the built-in, put a single
trailing underscore after it like "file_". That's just a style convention
though, you are free to call it FiLE ,or anything else, if you prefer.

(3) Last but not least, class attributes with two leading and trailing
underscores are considered special but public, like __init__ and __repr__.
It is probably a bad idea to invent your own.

--
Steven.

Dec 29 '06 #8
At Friday 29/12/2006 13:17, Steven D'Aprano wrote:
>X.__dict__
{'__module__ ': '__main__', '__doc__': None}
>X.__name__
'X'
>X.__bases__
()

Now that's interesting... if __name__ and __bases__ don't live in the
class __dict__, where do they live? What other methods and attributes are
invisibly in X?
They live in the C structure implementing the type; members tp_bases
and tp_name respectively. Most predefined type/class attributes have
an slot for storing its value, for efficiency. See include/object.h
--
Gabriel Genellina
Softlab SRL


_______________ _______________ _______________ _____
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Dec 30 '06 #9
Steven D'Aprano wrote:

>
There are three other underscore conventions in use:

(1) Objects with a single leading underscore like _attribute are private
by convention, but Python doesn't enforce it. Starting an object with a
single underscore is like writing "# Private! Don't touch!" after it.

(2) By convention, if you want to create a name that is the same as a
built-in object without shadowing (hiding) the built-in, put a single
trailing underscore after it like "file_". That's just a style convention
though, you are free to call it FiLE ,or anything else, if you prefer.

(3) Last but not least, class attributes with two leading and trailing
underscores are considered special but public, like __init__ and __repr__.
It is probably a bad idea to invent your own.
Very detailed. But I was attempting to debug some code which subclassed
other code. I got a traceback that something like "no
mySubClass.__so urce.query() did not exist". The superclass had something
like "myClass.__sour ce.query(sql)" which worked
but "mySubClass.__s ource.query(sql )" did not work. So I tried to debug
using "dir(myClass.__ source)" and got an error. And I also got error when
I "dir(mySubClass .__source". So how could I have debugged the problem if
dir() will not display information on the __source? I hope that explains
my issue.

Thanks
Johnf
Dec 30 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1616
by: Torsten Bronger | last post by:
Hallöchen! I have a file that looks a little bit like a C header file with a long list of variables (actually constants) definitions, e.g. VI_ATTR_TIMO = 0x54378 .... Actually I need this in a couple of low-level modules that are imported into the main module, and in the main module itself. They
1
4373
by: mark4asp | last post by:
What are the best methods for using global constants and variables? I've noticed that many people put all global constants in a file and include that file on every page. This is the best way of doing it - is it not? Once the application has loaded the page it is cached and is immediately available for other pages. With global variables - the best thing to do would be to use application variables - so long as there weren't too many...
2
9174
by: Patient Guy | last post by:
I have a library of functions representing a filesystem interface (essentially a file selection interface, to be used in opening/reading/writing/closing files). Heavily scripted HTML document #1, very application-like, must include the JS file for this library of functions. One reason is that it must call a function to name a "callback" function, and within its own script block, must define the callback function. The callback handles...
1
2044
by: Iyer, Prasad C | last post by:
Hi, I got a class which I need to serialize, except for couple of variable. i.e. import cPickle as p class Color: def __init__(self): print "hello world" self.x=10 self.somechar="this are the characters"
8
2058
by: Fernando Lopes | last post by:
Hi there! Someone has some code sample about when is recommend use a statis method? I know this methos don't want to be initialized and all but I want to know when I need to use it. Tks. Fernando
182
7575
by: Jim Hubbard | last post by:
http://www.eweek.com/article2/0,1759,1774642,00.asp
7
13715
by: misha | last post by:
Hello. I was wandering if someone could explain to me (or point to some manual) the process of mapping the addresses of host variables by DB2. Especially I would like to know when DB2 decides to reinitialize the addresses and even more when it decides not to do it. Recently I've ben strucked with a problem of host variables defined in LINKAGE SECTION, and it took me some time to find the cause and solution for the problem.
2
1140
by: tony obrien | last post by:
Hi, I have written a VB.Net Class Library to handle TCP message traffic between a client and a server. (i.e. I have exposed Props/Methods to a caller and handle the gory details regarding encryption and msg handling back and fro to the server in the DLL.) For a single client, this all works swell. Now I am endeavoring to create a LOAD TESTER which is nothing more than a
12
3848
by: MrHelpMe | last post by:
Hello again all, I've finished my whole application and now I don't like the whole session variables that I am using. I have a form, user fills in info clicks submit and using CDOSYSMail an email link gets created with an encoded query string. i.e http://www.yahoo.ca?#$@%@&#%#$@&^@%# which translates into http://www.yahoo.ca?userID=54&LocationID=Denver. Now when the user get's this email and clicks on the link I have a
3
1483
by: Immortal Nephi | last post by:
The rule of inheritance states that you define from top to bottom. Sometimes, you want to define base class and set reference from dervied class to base class, but you violate the rule. Here is an example of my code below. class A {}; class B : public A {}; int main(void) {
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10354
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10359
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10101
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6870
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3837
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.