Skip to content

Instantly share code, notes, and snippets.

@magicaltrevor
Last active December 15, 2015 17:19
Show Gist options
  • Save magicaltrevor/5295511 to your computer and use it in GitHub Desktop.
Save magicaltrevor/5295511 to your computer and use it in GitHub Desktop.
>>> def instiClass(name,attributes_dict,extends=None,functions_list=None):
... if extends:
... class klass(eval(extends)): pass
... else:
... class klass: pass
... for key,value in attributes_dict.items():
... setattr(klass, key, value)
... if functions_list:
... for func in functions_list:
... setattr(klass,func.__name__, func)
... klass.__name__ = name
... return klass
...
>>> def printHI(self):
... print "hi"
...
>>>
... def add(self, a, b):
... return a + b
...
>>>
... def subtract(self):
... return self.a - self.b
...
>>> attrs = {'test':'bar', 'hi':'there', 'a' : 5, 'b' : 4}
>>>
>>> funclist = [printHI, add, subtract]
>>>
>>> name = 'Foo'
>>> Foo = instiClass(name, attrs, None, funclist)
>>> test = Foo()
>>> test.a
5
>>> test.test
'bar'
>>> test.hi
'there'
>>> test.add(1,2)
3
>>> test.subtract()
1
>>> Foo
<class __main__.Foo at 0x7f7c5de6fef0>
>>> test
<__main__.Foo instance at 0x7f7c5de313b0>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment