Created
September 10, 2008 20:52
-
-
Save jeremyBanks/10064 to your computer and use it in GitHub Desktop.
[2010-01] me noobing out with python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
a = MyA() | |
b = MyB() | |
print a | |
print b | |
print a.x | |
print b.x | |
print a.xAndHam | |
print b.xAndHam | |
print a.xAndHam() | |
print b.xAndHam() | |
if __name__ == "__main__": main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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