Re: Postpone creation of attributes until needed

2007-06-13 Thread Steven D'Aprano
On Wed, 13 Jun 2007 02:15:11 -0700, Frank Millman wrote: > Thanks very much for all your attempts to help me, Steven. You have > succeeded in getting me to think properly about my problem and come up > with a much cleaner solution. I really appreciate it. Glad to be of help. -- Steven. -- ht

Re: Postpone creation of attributes until needed

2007-06-13 Thread Frank Millman
On Jun 13, 1:24 am, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > On Tue, 12 Jun 2007 08:53:11 -0700, Frank Millman wrote: > > Ah, if I had ever read that there were two instances involved, I hadn't > noticed. Sorry! > No problem - I really appreciate your input. I snipped the rest of your post, a

Re: Postpone creation of attributes until needed

2007-06-12 Thread Steven D'Aprano
On Tue, 12 Jun 2007 08:53:11 -0700, Frank Millman wrote: >> Since, as far as I can tell, there is no minimum time between creating the >> instance at (1) and trying to access instance.y at (2), there is no >> minimum time between (1) and calling compute() at (4), except for the >> execution time o

Re: Postpone creation of attributes until needed

2007-06-12 Thread Frank Millman
On Jun 12, 1:18 pm, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > On Mon, 11 Jun 2007 22:35:46 -0700, Frank Millman wrote: > > On Jun 12, 1:46 am, Steven D'Aprano > > <[EMAIL PROTECTED]> wrote: > > >> >> You haven't told us what the 'compute' method is. > > >> >> Or if you have, I missed it. > > >>

Re: Postpone creation of attributes until needed

2007-06-12 Thread Gabriel Genellina
En Tue, 12 Jun 2007 08:18:40 -0300, Steven D'Aprano <[EMAIL PROTECTED]> escribió: > On Mon, 11 Jun 2007 22:35:46 -0700, Frank Millman wrote: > >> Because, as I have tried to explain elsewhere (probably not very >> clearly), not all the information required to perform compute() is >> available at

Re: Postpone creation of attributes until needed

2007-06-12 Thread Steven D'Aprano
On Mon, 11 Jun 2007 22:35:46 -0700, Frank Millman wrote: > On Jun 12, 1:46 am, Steven D'Aprano > <[EMAIL PROTECTED]> wrote: >> >> >> You haven't told us what the 'compute' method is. >> >> >> Or if you have, I missed it. >> >> > Sorry - I made it more explicit above. It is the method that sets up

Re: Postpone creation of attributes until needed

2007-06-11 Thread Frank Millman
On Jun 12, 1:46 am, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > > >> You haven't told us what the 'compute' method is. > > >> Or if you have, I missed it. > > > Sorry - I made it more explicit above. It is the method that sets up > > all the missing attributes. No matter which attribute is referen

Re: Postpone creation of attributes until needed

2007-06-11 Thread Steven D'Aprano
On Mon, 11 Jun 2007 05:27:35 -0700, Frank Millman wrote: > I now have the following - > class A(object): > ...def __init__(self,x,y): > ...self.x = x > ...self.y = y > ...def __getattr__(self,name): > ...print 'getattr',name > ...self.compute() > ...

Re: Postpone creation of attributes until needed

2007-06-11 Thread Steven Bethard
George Sakkis wrote: > On Jun 11, 8:27 am, Frank Millman <[EMAIL PROTECTED]> wrote: >> On Jun 11, 1:56 pm, Steven D'Aprano >> >> <[EMAIL PROTECTED]> wrote: >> >>> Unless you have thousands and thousands of instances, __slots__ is almost >>> certainly not the answer. __slots__ is an optimization to

Re: Postpone creation of attributes until needed

2007-06-11 Thread Frank Millman
On Jun 11, 5:22 pm, George Sakkis <[EMAIL PROTECTED]> wrote: > On Jun 11, 10:37 am, Frank Millman <[EMAIL PROTECTED]> wrote: > > > You never *need* decorators, in the sense it's just syntax sugar for > things you might do without them, but they're handy once you get your > head around them. > > > A

Re: Postpone creation of attributes until needed

2007-06-11 Thread George Sakkis
On Jun 11, 10:37 am, Frank Millman <[EMAIL PROTECTED]> wrote: > On Jun 11, 3:38 pm, George Sakkis <[EMAIL PROTECTED]> wrote: > >The boilerplate code can be minimal too with an appropriate > > decorator, something like: > > > class A(object): > > > def __init__(self,x,y): > > self.x = x

Re: Postpone creation of attributes until needed

2007-06-11 Thread Frank Millman
On Jun 11, 3:38 pm, George Sakkis <[EMAIL PROTECTED]> wrote: > On Jun 11, 8:27 am, Frank Millman <[EMAIL PROTECTED]> wrote: > > > > Sorry - I made it more explicit above. It is the method that sets up > > all the missing attributes. No matter which attribute is referenced > > first, 'compute' sets

Re: Postpone creation of attributes until needed

2007-06-11 Thread George Sakkis
On Jun 11, 8:27 am, Frank Millman <[EMAIL PROTECTED]> wrote: > On Jun 11, 1:56 pm, Steven D'Aprano > > <[EMAIL PROTECTED]> wrote: > > > Unless you have thousands and thousands of instances, __slots__ is almost > > certainly not the answer. __slots__ is an optimization to minimize the > > size of ea

Re: Postpone creation of attributes until needed

2007-06-11 Thread Frank Millman
On Jun 11, 1:56 pm, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > > Unless you have thousands and thousands of instances, __slots__ is almost > certainly not the answer. __slots__ is an optimization to minimize the > size of each instance. The fact that it prevents the creation of new > attributes i

Re: Postpone creation of attributes until needed

2007-06-11 Thread Giles Brown
On 11 Jun, 11:10, Frank Millman <[EMAIL PROTECTED]> wrote: > On Jun 11, 11:47 am, Phil Thompson <[EMAIL PROTECTED]> > wrote: > > > > > On Monday 11 June 2007 10:24 am, Frank Millman wrote: > > > > Hi all > > > > I have a small problem. I have come up with a solution, but I don't > > > know if it is

Re: Postpone creation of attributes until needed

2007-06-11 Thread Peter Otten
Frank Millman wrote: > I tried that, but I get AttributeError: 'A' object has no attribute > '__dict__'. That's what you get for (ab)using __slots__ without understanding the implications ;) You can instead invoke the __getattr__() method of the superclass: super(A, self).__getattr__(name) Pet

Re: Postpone creation of attributes until needed

2007-06-11 Thread Steven D'Aprano
On Mon, 11 Jun 2007 03:58:16 -0700, Frank Millman wrote: >> By using slots, you're telling Python not to reserve space for a __dict__, >> which means that your class cannot create attributes on the fly. >> > > I understand that. In fact I was already using slots, as I was > concerned about the nu

Re: Postpone creation of attributes until needed

2007-06-11 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, Frank Millman wrote: > On Jun 11, 12:21 pm, Steven D'Aprano > <[EMAIL PROTECTED]> wrote: >> > I use __slots__ to catch any invalid attributes, otherwise I would get >> > a 'maximum recursion depth exceeded' error. >> >> That's the wrong solution to that problem. To avoid th

Re: Postpone creation of attributes until needed

2007-06-11 Thread Frank Millman
On Jun 11, 12:21 pm, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > On Mon, 11 Jun 2007 02:24:51 -0700, Frank Millman wrote: > > Hi all > > > I have a small problem. I have come up with a solution, but I don't > > know if it is a) safe, and b) optimal. > > > I have a class with a number of attributes

Re: Postpone creation of attributes until needed

2007-06-11 Thread Frank Millman
On Jun 11, 11:47 am, Phil Thompson <[EMAIL PROTECTED]> wrote: > On Monday 11 June 2007 10:24 am, Frank Millman wrote: > > > Hi all > > > I have a small problem. I have come up with a solution, but I don't > > know if it is a) safe, and b) optimal. > > > I have a class with a number of attributes, b

Re: Postpone creation of attributes until needed

2007-06-11 Thread Steven D'Aprano
On Mon, 11 Jun 2007 02:24:51 -0700, Frank Millman wrote: > Hi all > > I have a small problem. I have come up with a solution, but I don't > know if it is a) safe, and b) optimal. > > I have a class with a number of attributes, but for various reasons I > cannot assign values to all the attribute

Re: Postpone creation of attributes until needed

2007-06-11 Thread Phil Thompson
On Monday 11 June 2007 10:24 am, Frank Millman wrote: > Hi all > > I have a small problem. I have come up with a solution, but I don't > know if it is a) safe, and b) optimal. > > I have a class with a number of attributes, but for various reasons I > cannot assign values to all the attributes at _

Postpone creation of attributes until needed

2007-06-11 Thread Frank Millman
Hi all I have a small problem. I have come up with a solution, but I don't know if it is a) safe, and b) optimal. I have a class with a number of attributes, but for various reasons I cannot assign values to all the attributes at __init__ time, as the values depend on attributes of other linked c