Created
September 4, 2008 18:52
-
-
Save jeremyBanks/8846 to your computer and use it in GitHub Desktop.
[2010-01] an old example of how __slots__ works
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 | |
from __future__ import division, with_statement | |
import sys, os | |
class StrictPerson(object): | |
__slots__ = ["first", "last", "age"] | |
def __str__(self): | |
return "%s %s (%s)" % (self.first, self.last, self.age) | |
def main(): | |
me = StrictPerson() | |
try: | |
print me | |
except AttributeError: | |
"Because I haven't set any of the required attributes, obviously." | |
me.first = "Jeremy" | |
me.last = "Banks" | |
me.age = 19 | |
print me | |
try | |
me.likesPotatoes = True | |
except AttributeError: | |
"Because I haven't made a slot for that attribute!" | |
if __name__ == "__main__": sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment