最流行的方式是说什么样的信息应该被设置为我班级的属性?
例如,我有一个名为Human
的类,类的属性接受变量中的特定数据。
实现这一目标的最佳方法是什么?
class Human:
def __init__(self,name,legs,hands,age):
self.name = name #must be a string
self.legs = legs #must not be more than 2
self.hands = hands #same goes as legs above
self.age = age #must not be above 110
self.hair = hair #must be among hair_list = ['black','blonde','bold']
答案 0 :(得分:2)
处理托管属性的一种方法是使用descriptors。例如:
class Name(object):
def __get__(self, instance, owner):
return self._name
def __set__(self, instance, name):
if not isinstance(name, basestring):
raise ValueError('Name must be a string')
self._name = name
class Human(object):
name = Name()
def __init__(self, name):
self.name = name
演示:
>>> h = Human(10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test.py", line 15, in __init__
self.name = name
File "test.py", line 7, in __set__
raise ValueError('Name must be a string')
ValueError: Name must be a string
>>> h = Human('test')
>>> h.name
'test'
答案 1 :(得分:0)
我认为没有比明确表达你的意图更好的方法了,尽管也许你可以相信创造Human
- 对象的任何人都会理解你不能500岁。
我相信,当其中一个属性无法验证时,最pythonic方式是raise
一个有意义的异常。我也使用默认参数,这样您就不必再次输入最常见的用例..
class Human:
def __init__(self, name, legs=2, hands=2, age=0, hair='blonde'):
if not isinstance(name, str):
raise TypeError("Name must be a string")
self.name = name
if legs > 2:
raise ValueError("More than 2 legs")
self.legs = legs
if hands > 2:
raise ValueError("More than 2 hands")
self.hands = hands
if age > 110:
raise ValueError("What, are you immortal?!")
self.age = age
if hair not in ['black', 'blonde', 'bold']:
raise ValueError("I'm sorry your hair color is not cool enough")
self.hair = hair
示例:
In [15]: Human(123)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-15-9998e6013647> in <module>()
----> 1 Human(123)
<ipython-input-13-65da6758e98d> in __init__(self, name, legs, hands, age, hair)
2 def __init__(self, name, legs=2, hands=2, age=0, hair='blonde'):
3 if not isinstance(name, str):
----> 4 raise TypeError("Name must be a string")
5 self.name = name
6
TypeError: Name must be a string
另一个......
In [16]: Human("John", age=1500)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-16-09f5670add7c> in <module>()
----> 1 Human("John", age=1500)
<ipython-input-13-65da6758e98d> in __init__(self, name, legs, hands, age, hair)
17
18 if age > 110:
---> 19 raise ValueError("What, are you immortal?!")
20 self.age = age
21
ValueError: What, are you immortal?!