How to automate accessor definition?

kj no.email at please.post
Sun Mar 21 12:57:40 EDT 2010


In <639908184290880449.447600deets-nospam.web.de at news.hansenet.de> Diez B. Roggisch <deets at nospam.web.de> writes:

>You don't. Python is not Java. So just use instance attributes, and if
>you need bhavior when accessing an attribute, introduce a property. 


Just accessing attributes looks a bit dangerous to me, due to bugs
like typing

  i.typo = 'foo'

when what you meant is

  i.type = 'foo'

I tried fixing this by mucking with __setattr__, but I didn't hit
on a satisfactory solution (basically, I couldn't find a good,
self-maintaining, way to specify the attributes that were OK to
set from those that weren't).  Is there anything built-in?

Regarding properties, is there a built-in way to memoize them? For
example, suppose that the value of a property is obtained by parsing
the contents of a file (specified in another instance attribute).
It would make no sense to do this parsing more than once.  Is there
a standard idiom for memoizing the value once it is determined for
the first time?

Thanks!

~K



More information about the Python-list mailing list