Re: How to automate accessor definition?

2010-03-23 Thread Bruno Desthuilliers
John Posner a écrit : On 3/22/2010 11:44 AM, Bruno Desthuilliers wrote: Another (better IMHO) solution is to use a plain property, and store the computed value as an implementation attribute : @property def foo(self): cached = self.__dict__.get('_foo_cache') if cached is None: self._foo_cach

Re: How to automate accessor definition?

2010-03-22 Thread kj
In <4ba79040$0$22397$426a7...@news.free.fr> Bruno Desthuilliers writes: >kj a écrit : >> PS: BTW, this is not the first time that attempting to set an >> attribute (in a class written by me even) blows up on me. It's >> situations like these that rattle my grasp of attributes, hence my >> origi

Re: How to automate accessor definition?

2010-03-22 Thread John Posner
On 3/22/2010 11:44 AM, Bruno Desthuilliers wrote: Another (better IMHO) solution is to use a plain property, and store the computed value as an implementation attribute : @property def foo(self): cached = self.__dict__.get('_foo_cache') if cached is None: self._foo_cache = cached = self._some

Re: How to automate accessor definition?

2010-03-22 Thread Bruno Desthuilliers
kj a écrit : In Dennis Lee Bieber writes: On Sun, 21 Mar 2010 16:57:40 + (UTC), kj declaimed the following in gmane.comp.python.general: 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 content

Re: How to automate accessor definition?

2010-03-22 Thread Alf P. Steinbach
* kj: In Dennis Lee Bieber writes: On Sun, 21 Mar 2010 16:57:40 + (UTC), kj declaimed the following in gmane.comp.python.general: 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

Re: How to automate accessor definition?

2010-03-22 Thread kj
In Dennis Lee Bieber writes: >On Sun, 21 Mar 2010 16:57:40 + (UTC), kj >declaimed the following in gmane.comp.python.general: >> 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

Re: How to automate accessor definition?

2010-03-21 Thread kj
In <4ba66311$0$27838$c3e8...@news.astraweb.com> Steven D'Aprano writes: >Then, in your __init__ method, to initialise an attribute use: >self.__dict__['attr'] = value >to bypass the setattr. Ah, that's the trick! Thanks! ~K -- http://mail.python.org/mailman/listinfo/python-list

Re: How to automate accessor definition?

2010-03-21 Thread Steve Holden
Christian Heimes wrote: > Steve Holden wrote: >> You may well find that namedtuple is faster than what you put together >> yourself, as the collections module is implemented in C. > > But namedtuple isn't, Steve. Namedtuple is a class generator that > creates fast and efficient classes. > Ah, rig

Re: How to automate accessor definition?

2010-03-21 Thread Patrick Maupin
On Mar 21, 11:57 am, kj wrote: > > 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

Re: How to automate accessor definition?

2010-03-21 Thread Christian Heimes
Steve Holden wrote: > You may well find that namedtuple is faster than what you put together > yourself, as the collections module is implemented in C. But namedtuple isn't, Steve. Namedtuple is a class generator that creates fast and efficient classes. -- http://mail.python.org/mailman/listinfo

Re: How to automate accessor definition?

2010-03-21 Thread Steven D'Aprano
On Sun, 21 Mar 2010 16:57:40 +, kj wrote: > 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' That's the price you pay for using a dynamic language like Python with no declarations. But hon

Re: How to automate accessor definition?

2010-03-21 Thread kj
In <4ba58503$0$27838$c3e8...@news.astraweb.com> Steven D'Aprano writes: >On Sat, 20 Mar 2010 22:15:54 +, kj wrote: >> I need to create a class solely for the purpose of encapsulating a large >> number of disparate data items. >There's a built-in for that. It's called "dict". Syntax for ite

Re: How to automate accessor definition?

2010-03-21 Thread Steve Holden
kj wrote: > In Chris Rebert > writes: > >> On Sat, Mar 20, 2010 at 3:15 PM, kj wrote: >>> I need to create a class solely for the purpose of encapsulating >>> a large number of disparate data items. =C2=A0At the moment I have no >>> plans for any methods for this class other than the bazillion

Re: How to automate accessor definition?

2010-03-21 Thread kj
In Chris Rebert writes: >On Sat, Mar 20, 2010 at 3:15 PM, kj wrote: >> I need to create a class solely for the purpose of encapsulating >> a large number of disparate data items. =C2=A0At the moment I have no >> plans for any methods for this class other than the bazillion >> accessors require

Re: How to automate accessor definition?

2010-03-21 Thread kj
In <639908184290880449.447600deets-nospam.web...@news.hansenet.de> Diez B. Roggisch 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 t

Re: How to automate accessor definition?

2010-03-21 Thread Diez B . Roggisch
kj wrote: > > > > > > > I need to create a class solely for the purpose of encapsulating > a large number of disparate data items. At the moment I have no > plans for any methods for this class other than the bazillion > accessors required to access these various instance variables. > (In c

Re: How to automate accessor definition?

2010-03-20 Thread Steven D'Aprano
On Sat, 20 Mar 2010 22:15:54 +, kj wrote: > I need to create a class solely for the purpose of encapsulating a large > number of disparate data items. There's a built-in for that. It's called "dict". Syntax for item access is a tiny bit different, but still very common: data['foo'] instead

Re: How to automate accessor definition?

2010-03-20 Thread Someone Something
Just initialize everything in the constructor, unless you have *really *good reason not to do that. On Sat, Mar 20, 2010 at 6:54 PM, Chris Rebert wrote: > On Sat, Mar 20, 2010 at 3:15 PM, kj wrote: > > I need to create a class solely for the purpose of encapsulating > > a large number of dispara

Re: How to automate accessor definition?

2010-03-20 Thread Chris Rebert
On Sat, Mar 20, 2010 at 3:15 PM, kj wrote: > I need to create a class solely for the purpose of encapsulating > a large number of disparate data items.  At the moment I have no > plans for any methods for this class other than the bazillion > accessors required to access these various instance var

How to automate accessor definition?

2010-03-20 Thread kj
I need to create a class solely for the purpose of encapsulating a large number of disparate data items. At the moment I have no plans for any methods for this class other than the bazillion accessors required to access these various instance variables. (In case it matters, this class is mea