TL; DR:必须为每个属性定义一组唯一的getter和setter()' d变量很糟糕。我可以定义通用的getter和setter,并将它们用于我想要的任何变量吗?
我们说我和一些不错的吸气者和制定者一起上课:
class Foo
def getter(self):
return _bar+' sasquatch'
def setter(self, value):
_bar = value+' unicorns'
bar = property(getter, setter)
非常好,对吧?
现在让我说我输入另一个名为" baz"而且我不希望它被这个sasquatch / unicorn乐趣所遗忘。好吧,我想我可以制作另一组getter和setter:
class Foo
def bar_getter(self):
return _bar+' sasquatch'
def bar_setter(self, value):
_bar = value+' unicorns'
bar = property(bar_getter, bar_setter)
def baz_getter(self):
return _baz+' sasquatch'
def baz_setter(self, value):
_baz = value+' unicorns'
baz = property(baz_getter, baz_setter)
但那不是很干,而且不必要地弄乱了我的代码。我想我可以做一点DRYer:
class Foo
def unicornify(self, value):
return value+' unicorns'
def sasquatchify(self, value):
return value+' sasquatch'
def bar_getter(self):
return self.sasquatchify(_bar)
def bar_setter(self, value):
_bar = self.unicornify(_bar)
bar = property(bar_getter, bar_setter)
def baz_getter(self):
return self.sasquatchify(_baz)
def baz_setter(self, value):
_baz = self.unicornify(_baz)
baz = property(baz_getter, baz_setter)
虽然这可能会使我的代码DRYer,但它并不理想。如果我想unicornify和sasquatchify两个更多的变量,我将不得不再添加四个函数!
必须有更好的方法来做到这一点。我可以在多个变量中使用单个通用getter和/或setter吗?
Unicorn-less和sasquatch-less实际实现:我使用SQLAlchemy ORM,并希望在从数据库存储和检索数据时转换一些数据。一些转换适用于多个变量,我不想让我的类与getter和setter混乱。
答案 0 :(得分:12)
如何:
def sasquatchicorn(name):
return property(lambda self: getattr(self, name) + ' sasquatch',
lambda self, val: setattr(self, name, val + ' unicorns'))
class Foo(object):
bar = sasquatchicorn('_bar')
baz = sasquatchicorn('_baz')
更一般地说:
def sasquatchify(val):
return val + ' sasquatch'
def unicornify(val):
return val + ' unicorns'
def getset(name, getting, setting):
return property(lambda self: getting(getattr(self, name)),
lambda self, val: setattr(self, name, setting(val)))
class Foo(object):
bar = getset('_bar', sasquatchify, unicornify)
baz = getset('_baz', sasquatchify, unicornify)
或者,只需要更多的工作,就可以使用完整的描述符协议,如agf's answer中所述。
答案 1 :(得分:5)
这是descriptor protocol property
所基于的内容:
class Sasicorn(object):
def __init__(self, attr):
self.attr = "_" + attr
def __get__(self, obj, objtype):
return getattr(obj, self.attr) + ' sasquatch'
def __set__(self, obj, value):
setattr(obj, self.attr, value + ' unicorns')
class Foo(object):
def __init__(self, value = "bar"):
self.bar = value
self.baz = "baz"
bar = Sasicorn('bar')
baz = Sasicorn('baz')
foo = Foo()
foo2 = Foo('other')
print foo.bar
# prints bar unicorns sasquatch
print foo.baz
# prints baz unicorns sasquatch
print foo2.bar
# prints other unicorns sasquatch
虽然工厂功能中的property
可能适用于您的玩具示例,但听起来您可能需要更多控制您的实际用例。
答案 2 :(得分:2)
我的一位同事建议使用闭包来返回getter和setter函数,这是我决定使用的。
class Foo(object):
def setter(var):
def set(self, value):
setattr(self, var, value+' unicorn')
return set
def getter(var):
def get(self):
return getattr(self, var)+' sasquatch'
return get
bar = property(getter('_bar'), setter('_bar'))
f = Foo()
f.foo = 'hi'
print f.foo
但是,谢谢大家的答案:)
答案 3 :(得分:1)
使用getattribute和setattr,您可以为过去和将来的所有属性定义此内容。
class Foo(object):
x = 3
def __getattribute__(self, attr):
return str(object.__getattribute__(self, attr)) + ' sasquatch'
def __setattr__(self, attr, value):
object.__setattr__(self, attr, str(value) + ' unicorn')
print Foo.x
f = Foo()
print f.x
f.y = 4
print f.y
打印:
3
3 sasquatch
4 unicorn sasquatch
答案 4 :(得分:-1)
# coding=utf-8
__author__ = 'Ahmed Şeref GÜNEYSU'
class Student(object):
def __init__(self, **kwargs):
for k, v in kwargs.iteritems():
self.__setattr__(k, v)
if __name__ == '__main__':
o = Student(first_name='Ahmed Şeref', last_name='GÜNEYSU')
print "{0} {1}".format(o.first_name, o.last_name)
print o.email
给予
Ahmed Şeref GÜNEYSU
File "/Users/ahmed/PycharmProjects/sanbox/abstract_classes/object_initializer/__init__.py", line 13, in <module>
print o.email
AttributeError: 'Student' object has no attribute 'email'
Process finished with exit code 137