Fool Python class with imaginary members (serious guru stuff inside)
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Thu Sep 20 15:15:06 EDT 2012
On Thu, 20 Sep 2012 06:52:07 -0700, Jure Erznožnik wrote:
> 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.
Why do you hate your users so much that you want to cause them enormous
difficulty with perfectly reasonable code like this?
tmp = obj.member
x = tmp.another_member
> So the first method "detects" that we only need a simple value and
> returns that.
Fortunately that is impossible without nasty bytecode or AST hacks. Thank
the stars that Python doesn't allow anything as badly designed as this!
--
Steven
More information about the Python-list
mailing list