Skip to content

Instantly share code, notes, and snippets.

@eclecticmiraclecat
Created June 1, 2020 14:00
Show Gist options
  • Save eclecticmiraclecat/b8a9fb74765c16243210e3a34c747dbb to your computer and use it in GitHub Desktop.
Save eclecticmiraclecat/b8a9fb74765c16243210e3a34c747dbb to your computer and use it in GitHub Desktop.
>>> class Foo(object):
... def __init__(self, x):
... self.x = x
... def spam(self):
... print('spam')
...
>>> f = Foo(2)
>>> f.x
2
>>> f.spam()
spam
>>>
>>> name = 'Foo'
>>> bases = (object, )
>>> def __init__(self, x):
... self.x = x
...
>>> def spam(self):
... print('spam')
...
>>> methods = {'__init__': __init__, 'spam': spam}
>>>
>>> name
'Foo'
>>> bases
(<class 'object'>,)
>>> methods
{'__init__': <function __init__ at 0x7f95b61b0598>, 'spam': <function spam at 0x7f95b61b0730>}
>>> Foo = type(name, bases, methods)
>>> Foo
<class '__main__.Foo'>
>>> f = Foo(2)
>>> f.x
2
>>> f.spam()
spam
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment