Created
December 13, 2011 15:24
-
-
Save dketov/1472509 to your computer and use it in GitHub Desktop.
Атрибуты классов
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
# -*- encoding: utf-8 -*- | |
""" | |
Атрибуты класса | |
""" | |
class Bag: | |
def __init__(self): | |
self.data = [] | |
def add(self, x): | |
self.data.append(x) | |
def addtwice(self, x): | |
self.add(x) | |
self.add(x) | |
a = Bag() | |
a.add(4) |
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
# -*- encoding: utf-8 -*- | |
""" | |
Динамические атрибуты | |
""" | |
class empty: | |
def __getattr__(self, attrname): | |
if attrname == "age": | |
return 41 | |
else: | |
raise AttributeError, attrname | |
X = empty() | |
print X.age |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment