Defining classes

N

Nick Maclaren

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)

Does anyone know how I can achieve this? Naturally, I don't need
anything more than the address of the class in brinjal, as it won't
be used until after the class has been created properly. Actually,
it won't be used except to test for class equivalence, but I may
want to extend later.


Regards,
Nick Maclaren.
 
G

Gabriel Genellina

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(weeble)

Not perfect but manageable I think...


--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
 
D

Duncan Booth

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 the statements in the class body have executed. The usual way
to achieve what you want is to assign the static member from outside the
class.

class weeble:
pass

weeble.wumpus = brinjal(weeble)


Alternatively you can play tricks with metaclasses for a similar effect.
 
N

Nick Maclaren

|> >
|> > 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 the statements in the class body have executed. The usual way
|> to achieve what you want is to assign the static member from outside the
|> class.
|>
|> class weeble:
|> pass
|>
|> weeble.wumpus = brinjal(weeble)

Thanks (and to Gabriel Genellina). That is what I am doing, but it is
not ideal in other respects - for example, I want to make that item
read-only! It would be much cleaner not to have to fiddle with static
members after the class is initialised.

|> Alternatively you can play tricks with metaclasses for a similar effect.

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 :)

What I am trying to do is very simple, but is somewhat unusual. That
is the story of my life, so I am used to having problems.


Regards,
Nick Maclaren.
 
M

Michael Spencer

Nick said:
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 :)
... def __new__(cls):
... return 42
...
"instantiation" (i.e., calling the __new__ method) of new-style classes
can return whatever you like, but I'm not sure how that helps.

One way of having a class member refer to the class, is to use the
descriptor protocol, e.g.,:
... def __get__(self, obj, cls):
return brinjal(cls)

... ... wumpus = Brinjal()
...

Michael
 
N

Nick Maclaren

|>
|> "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 class declaration - and that is what I need in this context! My
request in this thread was because I am trying to find a way around
that restriction. All right, I am looking for a seriously advanced
language feature, that doesn't exist in 99% of known languages :)

I am very much out of touch with Lisp, but there was an almighty
hoo-hah over this point some 25 years back, during the design of
Common Lisp. I believe that it included it, and I am pretty sure
that I could do it in Haskell (which I am almost as out of touch
with), but in no other language that most people will ever have
heard of. But I am (again) out of touch with this area!

Ideally, what I want to do is to define a class (A) in Python that
has essentially no exposed methods, but which can be instantiated to
produce another class (B), which can then be used exactly like a
built-in class (which is what it is designed to be). Each such
instantiation of (A) would be a distinct class (B), derived from (A).
All of the methods of (B) would be inherited from 'hidden' methods
of (A), most would be in C (the language), and few would usable
directly on 'objects' of class (A).

There are several ways of doing this in Python, but I can't find
any that are as transparent to the user as I would really like.

There is method in my madness :)


Regards,
Nick Maclaren.
 
S

Steven Bethard

Nick said:
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 said:
Alternatively you can play tricks with metaclasses for a similar effect.

Nick said:
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 :)

How are you doing it currently? Here's a sort of minimalist option:
... def __metaclass__(name, bases, bodydict):
... cls = type(name, bases, bodydict)
... cls.wumpus = 'brinjal', cls
... return cls
... ('brinjal', <class '__main__.weeble'>)

Of course, it still takes four lines and a metaclass...

STeVe
 
M

Michele Simionato

Steven said:
How are you doing it currently? Here's a sort of minimalist option:

... def __metaclass__(name, bases, bodydict):
... cls = type(name, bases, bodydict)
... cls.wumpus = 'brinjal', cls
... return cls
...
('brinjal', <class '__main__.weeble'>)

Of course, it still takes four lines and a metaclass...

Well, 'type' is a metaclass, yes, but not a custom metaclass, so I
would say that you are
using the metaclass hook here, but not a "real" metaclass. Still
waiting for class decorators ...

Michele Simionato
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top