Properties for modules?
Steve Howell
showell30 at yahoo.com
Mon Jun 11 18:05:20 EDT 2007
--- Ed Leafe <ed at leafe.com> wrote:
> I have a simple module that reads in values from
> disk when imported,
> and stores them in attributes, allowing for code
> like:
>
> >>> import moduleFoo
> >>> print moduleFoo.someSetting
> 'the value'
>
> What I'd like to do is have a more property-like
> behavior, so that
> if they try to set the value of
> moduleFoo.someSetting, it also
> persists it to disk. But properties are really only
> useful in
> instances of classes; if I define 'someSetting' as a
> property at the
> module level, I get:
>
> >>> import moduleFoo
> >>> print moduleFoo.someSetting
> <property object at 0x78a990>
>
> Does anyone know any good tricks for getting
> property-like behavior
> here?
>
Sorry to stray from your question a bit, but you are
reminding me of something that happens a lot in my
coding. I'll code something as a module, then turn it
into a class. I usually do it early enough in the
ballgame that it's not a major undertaking, but it can
still be kind of tedious.
In a lot of ways modules and classes are
interchangable in how you use them, and I'm curious
what strategies other people adopt in making the
progression from modules to classes. For example, is
there some sort of magical way to turn a module into a
class, i.e. to make it less of a singleton? I promise
to horribly abuse such techniques till I learn
better...
Right now, when I make the conversion, I just kind of
mechanically indent a bunch of functions, put a
"class" statement on top of them, add some self-dots,
etc., but it feels kind of wrong at times.
____________________________________________________________________________________
Got a little couch potato?
Check out fun summer activities for kids.
http://search.yahoo.com/search?fr=oni_on_mail&p=summer+activities+for+kids&cs=bz
More information about the Python-list
mailing list