Created
August 4, 2008 18:00
-
-
Save jeremyBanks/3941 to your computer and use it in GitHub Desktop.
[2010-01] some simple examples of using properties in python, probably not idiomatic
This file contains 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 | |
import sys | |
class Example(object): | |
@property | |
def readOnlyProperty(self): | |
print "Getting five" | |
return 5 | |
__it = 6 | |
def getWriteableProperty(self): | |
print "Getting it" | |
return self.__it | |
def setWriteableProperty(self, value): | |
print "Setting it" | |
self.__it = value | |
writeableProperty = property(getWriteableProperty, setWriteableProperty) | |
def main(): | |
x = Example() | |
print x.readOnlyProperty | |
print x.writeableProperty | |
x.writeableProperty = 13 | |
print x.writeableProperty | |
if __name__ == "__main__": sys.exit(main()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment