Custom Search

Saturday, April 16, 2011

python Creating new types at runtime

Creating new types at runtime

>>> NewType = type("NewType", (object,), {"x": "hello"})
>>> n = NewType()
>>> n.x
"hello"

which is exactly the same as

>>> class NewType(object):
>>> x = "hello"
>>> n = NewType()
>>> n.x
"hello"

No comments:

Post a Comment