Fool Python class with imaginary members (serious guru stuff inside)

Jure Erznožnik jure.erznoznik at gmail.com
Thu Sep 20 09:52:07 EDT 2012


I'm trying to create a class that would lie to the user that a member is in some cases a simple variable and in other cases a class. The nature of the member would depend on call syntax like so:
1. x = obj.member #x becomes the "simple" value contained in member
2. x = obj.member.another_member #x becomes the "simple" value contained in first member's another_member.

So the first method "detects" that we only need a simple value and returns that. The second method "sees" that we need "member" as a class and returns that. Note that "simple type" could mean anything, from int to bitmap image.

I have determined that this is possible if I sacrifice the final member reference to the __call__ override using function-call syntax: 1. x = obj.member(). The call syntax returns the simple value and the other returns the class. It is also possible if I override the __xxxitem__ methods to simulate a dictionary.

However, I would like to use the "true member" access syntax if possible.

So, is it possible?



More information about the Python-list mailing list