Re: Defining classes

2006-12-15 Thread Michele Simionato
Steven Bethard wrote: > How are you doing it currently? Here's a sort of minimalist option: > > >>> class weeble(object): > ... def __metaclass__(name, bases, bodydict): > ... cls = type(name, bases, bodydict) > ... cls.wumpus = 'brinjal', cls > ...

Re: Defining classes

2006-12-14 Thread Steven Bethard
Nick Maclaren wrote: > I am defining a class, and I need to refer to that class when > setting up its static data - don't ask - like this: > > Class weeble : > wumpus = brinjal(weeble) Duncan Booth wrote: > Alternatively you can play tricks with metaclasses for a similar effect. Nick Maclare

Re: Defining classes

2006-12-14 Thread Nick Maclaren
In article <[EMAIL PROTECTED]>, "Michele Simionato" <[EMAIL PROTECTED]> writes: |> Nick Maclaren wrote: |> > It would be much cleaner not to have to fiddle with static |> > members after the class is initialised. |> |> You can hide the fiddling, for instance with the trick explained here: |> |>

Re: Defining classes

2006-12-14 Thread Nick Maclaren
In article <[EMAIL PROTECTED]>, Michael Spencer <[EMAIL PROTECTED]> writes: |> |> "instantiation" (i.e., calling the __new__ method) of new-style classes |> can return whatever you like, but I'm not sure how that helps. Yes and no. While it can return any value you like, it can't act as a clas

Re: Defining classes

2006-12-14 Thread Michele Simionato
Nick Maclaren wrote: > It would be much cleaner not to have to fiddle with static > members after the class is initialised. You can hide the fiddling, for instance with the trick explained here: http://www.phyast.pitt.edu/~micheles/python/classinitializer.html Michele Simionato -- http:/

Re: Defining classes

2006-12-13 Thread Michael Spencer
Nick Maclaren wrote: > > Well, I am already doing that, and regretting the fact that Python > doesn't seem to allow a class instantiation to return a new class :-) > >>> class Fake(object): ... def __new__(cls): ... return 42 ... >>> Fake() 42 >>> "instantiation" (i.e.

Re: Defining classes

2006-12-13 Thread Nick Maclaren
In article <[EMAIL PROTECTED]>, Duncan Booth <[EMAIL PROTECTED]> writes: |> > |> > I am defining a class, and I need to refer to that class when |> > setting up its static data - don't ask - like this: |> > |> > Class weeble : |> > wumpus = brinjal(weeble) |> |> You cannot refer to weeble u

Re: Defining classes

2006-12-13 Thread Duncan Booth
[EMAIL PROTECTED] (Nick Maclaren) wrote: > > I am defining a class, and I need to refer to that class when > setting up its static data - don't ask - like this: > > Class weeble : > wumpus = brinjal(weeble) You cannot refer to weeble until it has been created which isn't until after all of

Re: Defining classes

2006-12-13 Thread Gabriel Genellina
At Wednesday 13/12/2006 18:04, Nick Maclaren wrote: I am defining a class, and I need to refer to that class when setting up its static data - don't ask - like this: Class weeble : wumpus = brinjal(weeble) Move it below the class: class weeble: weeble.wumpus = brinjal(weebl

Re: defining classes

2005-09-02 Thread Steve Holden
LeRoy Lee wrote: > I have been searching for the answer to this as it will determine how I use > classes. Here are two bits of code. [snip already well-quoted examples] > > I can't figure out why it is working this way. I figure I must be thinking > about this wrong. I was thinking that I c

Re: defining classes

2005-09-02 Thread Steve Horsley
LeRoy Lee wrote: > I have been searching for the answer to this as it will determine how I > use classes. Here are two bits of code. > > class foo1: >def __init__(self, i): >self.r = i >self.j = 5 > >>> h = foo1(1) >>> h.r > > 1 > >>> h.j > > 5 > > > Now take this examp

Re: defining classes

2005-09-02 Thread Benji York
LeRoy Lee wrote: > I have been searching for the answer to this as it will determine how I use > classes. Here are two bits of code. > class foo2: > def __init__(self): > self.j = 5 > > >>>h = foo2() >>>h.j > > Traceback (most recent call last): > File "", line 1, in ? > Attribu

Re: defining classes

2005-09-02 Thread Grant Edwards
On 2005-09-02, LeRoy Lee <[EMAIL PROTECTED]> wrote: > Now take this example > > class foo2: > def __init__(self): > self.j = 5 > >>>h = foo2() >>>h.j > Traceback (most recent call last): > File "", line 1, in ? > AttributeError: foo2 instance has no attribute 'j' Works fine for me e

Re: defining classes

2005-09-02 Thread Michael Hoffman
LeRoy Lee wrote: > class foo2: >def __init__(self): >self.j = 5 > >>> h = foo2() >>> h.j > > Traceback (most recent call last): > File "", line 1, in ? > AttributeError: foo2 instance has no attribute 'j' Try again: >>> class foo2: ...def __init__(self): ...self.j = 5