Python descriptors are not transparent when they're first used

Seth Gordon sethg-Dp9fwfP21SfQT0dZR+AlfA at public.gmane.org
Wed Aug 1 14:05:24 EDT 2007


Cole Tuininga wrote:
> On Wed, 2007-08-01 at 12:49 -0400, Seth Gordon wrote:
> 
>>When I changed the code to take out the ` marks, I still got
>>"<WriteOnly.WriteOnly object ...>" in the last line of output.
> 
> 
> Hmmm - grab the code from http://www.code-energy.com/code/test.py - what
> do you get for output from that?

Double hmmm.

It looks like the distinction is this: if I have

class Foo(object):
    quux = WriteOnly('quux')

then there is one WriteOnly object shared among all instances of Foo,
and attempts to get and set any instance's quux attribute get
automagically translated.

But if I have

class Bar(object):
    def __init__(self):
        self.quux = WriteOnly('quux')

then each instance of Bar has its own WriteOnly object in its own quux
attribute, and no magic happens when accessing that attribute.

Which is a shame, because I had hoped I could factor out the repetition
of 'quux' in those code samples by passing in names at initialization
time and using "self.__dict__[attr] = WriteOnly(attr)" to set everything
up.  Maybe I can use some metaclass hackery to pull that off.  Or maybe
I should stop trying to be quite so clever.

Thanks for helping me figure this out.

> 


-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.






More information about the Discuss mailing list