Anonymous Classes

L

Lachlan Gunn

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.
 
B

Bruno Desthuilliers

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

.... pass
........ def __init__(self, what):
.... self._toto = type('Autototo', (Toto,), dict(_what=what))
....1138
 
L

Lachlan Gunn

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.
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top