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

Discussion in 'Python' started by Jure Erznožnik, Sep 20, 2012.

  1. I'm trying to create a class that would lie to the user that a member is insome cases a simple variable and in other cases a class. The nature of themember 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 returnsthat. 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?
     
    Jure Erznožnik, Sep 20, 2012
    #1
    1. Advertising

  2. Jure Erznožnik

    Terry Reedy Guest

    Re: Fool Python class with imaginary members (serious guru stuffinside)

    On 9/20/2012 9:52 AM, 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.


    x.y.z is parsed and executed as (x.y).z, so you are asking if the
    attribute-getter can know what will be done with the object it returns.
    Assuming CPython, you would have to write something that searches the
    Python code before compilation, the ast during compilation, or the
    bytecode after compilation.

    Much easier would be to define a union class that is a simple type with
    attributes and return that in the first lookup.

    class AttrInt(int):
    def __getattr__(self, name): return 'attribute'

    y = AttrInt(3)
    print(y, y.a)
    ###
    3 attribute

    If x.y returns an AttrInt, it will act like an int for most purposes,
    while x.y.z will return whatever AttrInt.__getattr__ does and the
    temporary AttrInt y disappears.

    --
    Terry Jan Reedy
     
    Terry Reedy, Sep 20, 2012
    #2
    1. Advertising

  3. Re: Fool Python class with imaginary members (serious guru stuffinside)

    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
     
    Steven D'Aprano, Sep 20, 2012
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark
    Replies:
    4
    Views:
    377
    Rick Spiewak
    Apr 26, 2004
  2. gibber
    Replies:
    1
    Views:
    276
    David Goodger
    Oct 28, 2004
  3. How to fool getpass()

    , Aug 15, 2006, in forum: C Programming
    Replies:
    2
    Views:
    370
    jmcgill
    Aug 15, 2006
  4. Gabriel
    Replies:
    0
    Views:
    271
    Gabriel
    Dec 5, 2007
  5. Gabriel

    Ubú : Suite of the imaginary beings

    Gabriel, Dec 5, 2007, in forum: C Programming
    Replies:
    0
    Views:
    289
    Gabriel
    Dec 5, 2007
Loading...

Share This Page