search by tags

for the user

adventures into the land of the command line

dynamic anonymous classes & functions in python

you can create a class like this:

>>> class X(object):
...     a = 1
...
>>> print X.a
1

but an anonymous one also like this:

>>> X = type('X', (object,), dict(a=1))
>>> print X.a
1

you can create a function like this:

>>> def double(x):
...     return x*2
...
>>> print double(2)
4

but an anonymous one also like this:

>>> double = lambda x: x * 2
>>> print double(2)
4

kewl