hide object property from dir() function?

J

jerryji

Hi,

Sorry for this newbie question, I was puzzled why the existing
property of an object is not shown in the dir() function output.

"v" is an lxml Element object variable --

In [44]: v
Out[44]: <Element 'documentProperties' at 0x8363cf8>

In [45]: dir(v)
Out[45]:
['__copy__',
'__deepcopy__',
'__reduce__',
'append',
'clear',
'find',
'findall',
'findtext',
'get',
'getchildren',
'getiterator',
'insert',
'items',
'keys',
'makeelement',
'remove',
'set']

dir() output doesn't contain the ".tag", which does exist --

In [46]: v.tag
Out[46]: 'documentProperties'

what is the rule governing the display of a property from dir()?

Many thanks in advance!

Jerry
 
M

Matimus

Hi,

Sorry for this newbie question, I was puzzled why the existing
property of an object is not shown in the dir() function output.

"v" is an lxml Element object variable --

In [44]: v
Out[44]: <Element 'documentProperties' at 0x8363cf8>

In [45]: dir(v)
Out[45]:
['__copy__',
'__deepcopy__',
'__reduce__',
'append',
'clear',
'find',
'findall',
'findtext',
'get',
'getchildren',
'getiterator',
'insert',
'items',
'keys',
'makeelement',
'remove',
'set']

dir() output doesn't contain the ".tag", which does exist --

In [46]: v.tag
Out[46]: 'documentProperties'

what is the rule governing the display of a property from dir()?


You can also make properties by modifying the __getattr__ and
__setattr__ methods of a class. When done that way it won't show up
when dir is called. So it isn't necessarily a rule, just a different
way of implementing a property.

Matt
 
T

Tim Golden

jerryji said:
Sorry for this newbie question, I was puzzled why the existing
property of an object is not shown in the dir() function output.

The under-development version of Python (2.6) allows for a
__dir__ magic method by which the class implementer can
return whatever he wishes from a dir (). This is to help,
for example, modules like my WMI one which makes heavy use
of __getattr__ magic to proxy across Windows COM attributes.
This, in turn, helps editors and IDEs which can provide
popup lists of attributes etc.

All that said, I don't believe it takes any automatic
account of properties.

TJG

<noddy code example>
class X (object):
def __init__ (self, a):
self.a = a

print dir (X (1))

def __dir__ (self):
return ['x', 'y', 'z']

X.__dir__ = __dir__

print dir (X (2))

</code>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top