473,387 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Defining classes


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.
Dec 14 '06 #1
9 1218
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
Dec 14 '06 #2
nm**@cus.cam.ac.uk (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.
Dec 14 '06 #3

In article <Xn*************************@127.0.0.1>,
Duncan Booth <du**********@invalid.invalidwrites:
|
| 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.
Dec 14 '06 #4
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

Dec 14 '06 #5
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

Dec 14 '06 #6

In article <ma***************************************@python. org>,
Michael Spencer <ma**@telcopartners.comwrites:
|>
|"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.
Dec 14 '06 #7

In article <11*********************@80g2000cwy.googlegroups.c om>,
"Michele Simionato" <mi***************@gmail.comwrites:
|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.
Dec 14 '06 #8
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
Dec 14 '06 #9
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

Dec 15 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Harry Pehkonen | last post by:
I have been defining new class methods when I'm trying to simplify some code. But I'm thinking I should just define functions within that method because they aren't useful from the outside anyway....
1
by: Erik Max Francis | last post by:
I've come across a limitation in unpickling certain types of complex data structures which involve instances that override __hash__, and was wondering if it was known (basic searches didn't seem to...
12
by: Matt Garman | last post by:
I'd like to create a "custom output facility". In other words, I want an object whose use is similar to std::cout/std::cerr, but offers more flexibility. Instead of simply writing the parameter...
8
by: johny smith | last post by:
If I have a simple class with say a couple of integers only is there any need for me to provide a destructor? thanks!
9
by: Aguilar, James | last post by:
I know that one can define an essentially unlimited number of classes in a file. And one can declare just as many in a header file. However, the question I have is, should I? Suppose that, to...
5
by: Xiangliang Meng | last post by:
Hi, all. What are the benefit and the drawback of defining a class embedded inside another class? For example: class List { public:
8
by: Claudio Grondi | last post by:
It is maybe not a pure Python question, but I think it is the right newsgroup to ask for help, anyway. After connecting a drive to the system (via USB or IDE) I would like to be able to see...
2
by: Alex Feldman | last post by:
Which of the following is better? Defining an enum type inside a class as a nested type, or in the the namespace? An example of nested type enumerated type would be: public Class Product...
26
by: Cliff Williams | last post by:
Can someone explain the pros/cons of these different ways of creating a class? // 1 function myclass() { this.foo1 = function() {...} } // 2a
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.