Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #29552

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

Newsgroups comp.lang.python
Date 2012-09-20 06:52 -0700
Message-ID <89b80b56-d1f2-477e-b28a-4410f2cf9de6@googlegroups.com> (permalink)
Subject Fool Python class with imaginary members (serious guru stuff inside)
From Jure Erznožnik <jure.erznoznik@gmail.com>

Show all headers | View raw


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?

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Fool Python class with imaginary members (serious guru stuff inside) Jure Erznožnik <jure.erznoznik@gmail.com> - 2012-09-20 06:52 -0700
  Re: Fool Python class with imaginary members (serious guru stuff inside) Terry Reedy <tjreedy@udel.edu> - 2012-09-20 13:04 -0400
  Re: Fool Python class with imaginary members (serious guru stuff inside) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-20 19:15 +0000

csiph-web