class declaration shortcut

Luis M. González luismgz at gmail.com
Wed Feb 28 14:26:41 EST 2007


I've come across a code snippet in www.rubyclr.com where they show how
easy it is to declare a class compared to equivalent code in c#.
I wonder if there is any way to emulate this in Python.

The code is as follows:

Person = struct.new( :name, :birthday, :children)

I tried something like this, but it's nothing close to what I'd like:

def klass(table, *args):
	cls = new.classobj(table, (), {})
	for i in args:
		setattr(cls, i, i)
	return cls

But this above is not what I want.
I guess I should find a way to include the constructor code inside
this function, but I don't know if this is possible.
Also, I wonder if there is a way to use the variable name in order to
create a class with the same name (as in "Person"above).

Well, if anyone has an idea, I'd like to know...

Luis




More information about the Python-list mailing list