By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,713 Members | 764 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,713 IT Pros & Developers. It's quick & easy.

Re: Question regarding re module

P: n/a
Diez,

Thanks, you're right.
Delegated attributes are not in the dir() result.
>>getattr(reg, "pattern")
'[0-9]+'
>>getattr(reg, "flags")
0
>>getattr(reg, "groupindex")
{}

Tomohiro Kusumi
Tomohiro Kusumi schrieb:
>Hi,

I have a question regarding re module.
# By the way I'm not in this list, so I'm sorry but please CC me.

I tried following code in Python shell using a regular expression.
Why doesn't the result of dir(reg) have 'pattern', 'flags', and
'groupindex' although they exist as members of _sre.SRE_Pattern
instance ?

It sort of irritates me, because whenever I write Python code
using a module which I'm not used to using, I often try Python
shell with TAB complete to find out the member of module/instance.
It could be that the result overloads the __getattr__-method to delegate
calls to some object. Thus it's not part of the outer instance.

Diez


Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Jun 5, 7:11*am, Tomohiro Kusumi <kusumi.tomoh...@jp.fujitsu.com>
wrote:
>
It could be that the result overloads the __getattr__-method to delegate
calls to some object. Thus it's not part of the outer instance.
Didn't I read that Py3 will support a __dir__ method so that classes
*could* report such pseudo-attributes in response to dir?

-- Paul
Jun 27 '08 #2

P: n/a
Paul McGuire wrote:
On Jun 5, 7:11 am, Tomohiro Kusumi <kusumi.tomoh...@jp.fujitsu.com>
wrote:
>>It could be that the result overloads the __getattr__-method to delegate
calls to some object. Thus it's not part of the outer instance.

Didn't I read that Py3 will support a __dir__ method so that classes
*could* report such pseudo-attributes in response to dir?
It's already there in 2.6:

.... class X (object):
.... def __init__ (self, a, b):
.... self.a = a
.... self.b = b
.... def __dir__ (self):
.... return ['a', 'b', 'c']
....
>>dir (X (1, 2))
['a', 'b', 'c']
>>>
TJG
Jun 27 '08 #3

P: n/a
It could be that the result overloads the __getattr__-method to delegate
calls to some object. Thus it's not part of the outer instance.

Didn't I read that Py3 will support a __dir__ method so that classes
*could* report such pseudo-attributes in response to dir?

-- Paul

I don't believe that it possibly could:

class foo(object):

def __getattr__(self, what):
if what[0] == 't':
return True

f = foo()

print f.toby
** Posted from http://www.teranews.com **
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.