Skip to content

Instantly share code, notes, and snippets.

@jeremyBanks
Created September 10, 2008 20:52
Show Gist options
  • Save jeremyBanks/10064 to your computer and use it in GitHub Desktop.
Save jeremyBanks/10064 to your computer and use it in GitHub Desktop.
[2010-01] me noobing out with python
#!/usr/bin/env python
# encoding: utf-8
# Experimenting with creating a class without using the class statement.
# The classes MyA and MyB should be equivalent.
class MyA(object):
x = 5
def xAndHam(self):
return "%i HAM" % self.x
MyB = type("MyB", (object,), {"x": 5, "xAndHam": lambda self: "%i HAM" % self.x})
def main():
print MyA
print MyB
print
a = MyA()
b = MyB()
print a
print b
print
print a.x
print b.x
print
print a.xAndHam
print b.xAndHam
print
print a.xAndHam()
print b.xAndHam()
if __name__ == "__main__": main()
<class '__main__.MyA'>
<class '__main__.MyB'>
<__main__.MyA object at 0x31c0b0>
<__main__.MyB object at 0x31c0f0>
5
5
<bound method MyA.xAndHam of <__main__.MyA object at 0x31c0b0>>
<bound method MyB.<lambda> of <__main__.MyB object at 0x31c0f0>>
5 HAM
5 HAM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment