473,507 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Anonymous Classes

Hello.

I have a library (SQLObject) that stores data as class variables. I would
like to set a class variable in a certain context, specific to a certain
instance of an object. This would require some sort of anonymous class. I
have attempted to use the following code to set the connection string:

| class SQLStorage:
| def __init__(self, c, debug = False):
| config = StorageConfiguration(c)
|
| connection = sqlobject.connectionForURI(config.databaseString)
| if debug:
| connection.debug = True
|
| # I don't know whether this is right. My belief is that we can
| # subclass each table and use _connection on them.
| class newDatum(DatumTable):
| _connection = connection
|
| class newMetadatum(MetadatumTable):
| _connection = connection
|
| class newChecksum(ChecksumTable):
| _connection = connection
|
| self.__SQLDatum = newDatum
| self.__SQLMetadatum = newMetadatum
| self.__SQLChecksum = newChecksum

This does not work; Python complains that the classes already exist when
SQLObject is instantiated for a second time. This has led me to try
instantiating a subclass using DatumTable.__class__ and friends, but this
requires setting a class name, which, again, limits me to a single
instance. I *could* have a counter that appends a number to each class
name, but that's a fairly ugly solution; is it possible to create an
anonymous class in Python?

Thanks,
Lachlan.
Jul 12 '07 #1
2 2497
Lachlan Gunn a écrit :
Hello.

I have a library (SQLObject) that stores data as class variables. I would
like to set a class variable in a certain context, specific to a certain
instance of an object. This would require some sort of anonymous class. I
have attempted to use the following code to set the connection string:

| class SQLStorage:
You probably want:

class SQLStorage(object):
| def __init__(self, c, debug = False):
| config = StorageConfiguration(c)
|
| connection = sqlobject.connectionForURI(config.databaseString)
| if debug:
| connection.debug = True
|
| # I don't know whether this is right. My belief is that we can
| # subclass each table and use _connection on them.
| class newDatum(DatumTable):
| _connection = connection
|
| class newMetadatum(MetadatumTable):
| _connection = connection
|
| class newChecksum(ChecksumTable):
| _connection = connection
|
| self.__SQLDatum = newDatum
| self.__SQLMetadatum = newMetadatum
| self.__SQLChecksum = newChecksum
Are you sure you need name mangling here ?
This does not work; Python complains that the classes already exist when
SQLObject is instantiated for a second time. This has led me to try
instantiating a subclass using DatumTable.__class__ and friends, but this
requires setting a class name, which, again, limits me to a single
instance. I *could* have a counter that appends a number to each class
name, but that's a fairly ugly solution; is it possible to create an
anonymous class in Python?
Yes. Using type() :

Help on class type in module __builtin__:

class type(object)
| type(object) -the object's type
| type(name, bases, dict) -a new type

>>class Toto(object):
.... pass
....
>>class Tutu(object):
.... def __init__(self, what):
.... self._toto = type('Autototo', (Toto,), dict(_what=what))
....
>>t = Tutu(42)
t._toto
<class '__main__.Autototo'>
>>t._toto._what
42
>>t._toto()
<__main__.Autototo object at 0xb7c3806c>
>>t2 = Tutu(1138)
t2._toto
<class '__main__.Autototo'>
>>t2._toto._what
1138
Jul 12 '07 #2
Thanks for your help.

After trying your suggestion for a while, I eventually realised that
SQLObject was doing something funny in its metaclass that was causing the
issue, which I got around by mangling the class names with a counter.
Jul 12 '07 #3

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

Similar topics

3
4811
by: Ed Severn | last post by:
I'm sorry of this question has been posed and answered many times before. But I have avoided using the "package" statement because of this. Most of my classes have no "package" statement, and...
0
2053
by: Carlos Ribeiro | last post by:
I thought about this problem over the weekend, after long hours of hacking some metaclasses to allow me to express some real case data structures as Python classes. I think that this is something...
11
3267
by: G Fernandes | last post by:
I've heard some mention "anonymous" objects in c.l.c and other places (conversations), and I was wondering what exactly these are. Anonymous seems to imply that there is no name, but no name...
0
1633
by: Cordell Lawrence | last post by:
Okay guys, We are wondering if this is a bug in Framework 2.0.40607 and looking for some clarification on the issue. Take a look at the folowing code. public delegate bool BoundryTest(int...
4
9499
by: No One | last post by:
Does anyone know if or when anonymous class support will be added to C#?
6
6972
by: Gaijinco | last post by:
I have always felt that there are a lot of topics that you learned the facts but you only grasp the matter sometime down the road. For me, two of those topics are inner classes and anonymous...
6
2178
by: 2b|!2b==? | last post by:
I have the following declaration in a header file (MyClass.h) //contents of MyClass.h class MyClass { public: MyClass() MyClass(const MyClass&); ... };
0
1407
by: Peter Duniho | last post by:
On Mon, 01 Sep 2008 16:14:10 -0700, Blip <blip@krumpli.comwrote: Briefly, an anonymous method is exactly that: a method without a name. When you use the "delegate" keyword to declare an...
8
1756
by: Tim Chase | last post by:
I've got code similar to the following class Action: def __init__(self, ...): pass def __call__(self, ...): pass def get_help(self, ...): pass class Backend: class _Load(Action): def...
0
7223
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
7114
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
7321
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
7377
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...
1
7034
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.