Boston Linux & Unix (BLU) Home | Calendar | Mail Lists | List Archives | Desktop SIG | Hardware Hacking SIG
Wiki | Flickr | PicasaWeb | Video | Maps & Directions | Installfests | Keysignings
Linux Cafe | Meeting Notes | Blog | Linux Links | Bling | About BLU

BLU Discuss list archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Python descriptors are not transparent when they're first used



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.







BLU is a member of BostonUserGroups
BLU is a member of BostonUserGroups
We also thank MIT for the use of their facilities.

Valid HTML 4.01! Valid CSS!



Boston Linux & Unix / webmaster@blu.org