Skip to content

Instantly share code, notes, and snippets.

@jeremyBanks
Created September 4, 2008 18:52
Show Gist options
  • Save jeremyBanks/8846 to your computer and use it in GitHub Desktop.
Save jeremyBanks/8846 to your computer and use it in GitHub Desktop.
[2010-01] an old example of how __slots__ works
#!/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