Skip to content

Instantly share code, notes, and snippets.

@dketov
Created December 13, 2011 15:24
Show Gist options
  • Save dketov/1472509 to your computer and use it in GitHub Desktop.
Save dketov/1472509 to your computer and use it in GitHub Desktop.
Атрибуты классов
# -*- 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)
# -*- 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