Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Defining classes

Reply
Thread Tools

Defining classes

 
 
Nick Maclaren
Guest
Posts: n/a
 
      12-13-2006

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.
 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      12-13-2006
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(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
 
Reply With Quote
 
 
 
 
Duncan Booth
Guest
Posts: n/a
 
      12-13-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) (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 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.
 
Reply With Quote
 
Nick Maclaren
Guest
Posts: n/a
 
      12-13-2006

In article <Xns9898DF2734FF2duncanbooth@127.0.0.1>,
Duncan Booth <(E-Mail Removed)> 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 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.
 
Reply With Quote
 
Michael Spencer
Guest
Posts: n/a
 
      12-13-2006
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., 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 brinjal(cls): return cls.__name__

...
>>> class Brinjal(object): # must be new-style

... def __get__(self, obj, cls):
return brinjal(cls)

...
>>> class Weeble(object): # should be new-style

... wumpus = Brinjal()
...
>>> Weeble.wumpus

'Weeble'
>>> Weeble().wumpus

'Weeble'
>>>



Michael

 
Reply With Quote
 
Michele Simionato
Guest
Posts: n/a
 
      12-14-2006
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...itializer.html


Michele Simionato

 
Reply With Quote
 
Nick Maclaren
Guest
Posts: n/a
 
      12-14-2006

In article <(E-Mail Removed)>,
Michael Spencer <(E-Mail Removed)> 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 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.
 
Reply With Quote
 
Nick Maclaren
Guest
Posts: n/a
 
      12-14-2006

In article <(E-Mail Removed) om>,
"Michele Simionato" <(E-Mail Removed)> 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:
|>
|> http://www.phyast.pitt.edu/~micheles...itializer.html

Thanks very much. I shall definitely read that, to gain more understanding
if nothing else.


Regards,
Nick Maclaren.
 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      12-14-2006
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 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


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
... return cls
...
>>> weeble.wumpus

('brinjal', <class '__main__.weeble'>)

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

STeVe
 
Reply With Quote
 
Michele Simionato
Guest
Posts: n/a
 
      12-15-2006
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
> ... return cls
> ...
> >>> weeble.wumpus

> ('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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Defining many classes in a single file Aguilar, James C++ 9 07-14-2004 08:29 PM
defining or not defining destructors johny smith C++ 8 07-02-2004 08:51 AM
Defining constants in classes Joe C++ 29 03-04-2004 03:03 PM
CSS - Syntax for defining Links with Classes Ben Amada HTML 8 01-16-2004 06:38 AM
Defining classes in jscript Henke ASP .Net 3 11-28-2003 04:18 PM



Advertisments