class方法继承并覆盖class参数
classmethod inheritance and overwrite class parameter
当 PersonProfile 继承自 PersonBase 时,我很难理解如何通过 class 方法为 PersonProfile 赋值。
class PersonBase:
def __init__(self, contact_no=None, email=None, house=None, category=None, near=None, house_number=None, road=None,
unit=None, level=None, staircase=None, entrance=None, po_box=None, postcode=None, suburb=None,
city_district=None, city=None, island=None, state_district=None, state=None, country_region=None,
country=None, world_region=None):
self.contact_no = contact_no
self.email = email
self.house = house
self.category = category
self.near = near
self.house_number = house_number
self.road = road
self.unit = unit
self.level = level
self.staircase = staircase
self.entrance = entrance
self.po_box = po_box
self.postcode = postcode
self.suburb = suburb
self.city_district = city_district
self.city = city
self.island = island
self.state_district = state_district
self.state = state
self.country_region = country_region
self.country = country
self.world_region = world_region
class PersonProfile(PersonBase):
@classmethod
def from_string(cls, full_address):
"""
Takes raw, unformatted address string and creates a PersonProfile class out of it.
:param full_address: unformatted address coming from a DB
:type full_address: str
:return: PersonProfile class
:rtype: PersonProfile
"""
if full_address == "":
# return empty PersonProfile cls with all its defaults
return cls(PersonProfile)
elif full_address is None:
raise TypeError("Provided object must be of type string")
# extract phone numbers
_contact_no = PersonProfile.find_phone_no(full_address)
if len(_contact_no) != 0:
cls.contact_no = ",".join(_contact_no)
for c in _contact_no:
full_address = full_address.replace(c, '')
return cls()
因此,当我尝试使用 cls.contact_no = ",".join(_contact_no)
将 phone 编号分配给 PersonProfile.contact_no 时,它似乎没有任何效果。这样做的正确方法是什么?
此外,在该 class 方法结束时,我将如何 return 所有值,包括我在 class 方法执行期间覆盖的值?我正在使用 return cls()
,但似乎也没有用。
我想让它像这样工作:
p1 = PersonProfile.from_string("(+22) 936107349")
print(p1.contact_no)
--> (+22) 936107349
print(p1.city)
--> None
您想将解析后的数据提供给__init__
方法;通过调用 cls(**key_word_arguments)
,staticmethod
parse_full_address
返回的 kwargs
的字典被传递给构造函数。
我建议你写一个parser
,和class工厂分离出来的数据,validators
,传给工厂的argument已经彻底提取出来了。
class PersonProfile(PersonBase):
@classmethod
def from_string(cls, full_address):
"""
Takes raw, unformatted address string and creates a PersonProfile class out of it.
:param full_address: unformatted address coming from a DB
:type full_address: str
:return: PersonProfile class
:rtype: PersonProfile
"""
kwargs = PersonProfile.parse_full_address(full_address)
return cls(**kwargs)
@staticmethod
def parse_full_address(full_address):
"""parses the full_address and returns the parsed data as a dictionary
currently a stub that returns test data
"""
return {'contact_no': '(+22) 936107349', 'email': 'a@a.com', 'house': 'Ze Mansion', 'city': 'Dummy Data'}
p1 = PersonProfile.from_string("(+22) 936107349")
print(p1.contact_no)
print(p1.city)
当 PersonProfile 继承自 PersonBase 时,我很难理解如何通过 class 方法为 PersonProfile 赋值。
class PersonBase:
def __init__(self, contact_no=None, email=None, house=None, category=None, near=None, house_number=None, road=None,
unit=None, level=None, staircase=None, entrance=None, po_box=None, postcode=None, suburb=None,
city_district=None, city=None, island=None, state_district=None, state=None, country_region=None,
country=None, world_region=None):
self.contact_no = contact_no
self.email = email
self.house = house
self.category = category
self.near = near
self.house_number = house_number
self.road = road
self.unit = unit
self.level = level
self.staircase = staircase
self.entrance = entrance
self.po_box = po_box
self.postcode = postcode
self.suburb = suburb
self.city_district = city_district
self.city = city
self.island = island
self.state_district = state_district
self.state = state
self.country_region = country_region
self.country = country
self.world_region = world_region
class PersonProfile(PersonBase):
@classmethod
def from_string(cls, full_address):
"""
Takes raw, unformatted address string and creates a PersonProfile class out of it.
:param full_address: unformatted address coming from a DB
:type full_address: str
:return: PersonProfile class
:rtype: PersonProfile
"""
if full_address == "":
# return empty PersonProfile cls with all its defaults
return cls(PersonProfile)
elif full_address is None:
raise TypeError("Provided object must be of type string")
# extract phone numbers
_contact_no = PersonProfile.find_phone_no(full_address)
if len(_contact_no) != 0:
cls.contact_no = ",".join(_contact_no)
for c in _contact_no:
full_address = full_address.replace(c, '')
return cls()
因此,当我尝试使用 cls.contact_no = ",".join(_contact_no)
将 phone 编号分配给 PersonProfile.contact_no 时,它似乎没有任何效果。这样做的正确方法是什么?
此外,在该 class 方法结束时,我将如何 return 所有值,包括我在 class 方法执行期间覆盖的值?我正在使用 return cls()
,但似乎也没有用。
我想让它像这样工作:
p1 = PersonProfile.from_string("(+22) 936107349")
print(p1.contact_no)
--> (+22) 936107349
print(p1.city)
--> None
您想将解析后的数据提供给__init__
方法;通过调用 cls(**key_word_arguments)
,staticmethod
parse_full_address
返回的 kwargs
的字典被传递给构造函数。
我建议你写一个parser
,和class工厂分离出来的数据,validators
,传给工厂的argument已经彻底提取出来了。
class PersonProfile(PersonBase):
@classmethod
def from_string(cls, full_address):
"""
Takes raw, unformatted address string and creates a PersonProfile class out of it.
:param full_address: unformatted address coming from a DB
:type full_address: str
:return: PersonProfile class
:rtype: PersonProfile
"""
kwargs = PersonProfile.parse_full_address(full_address)
return cls(**kwargs)
@staticmethod
def parse_full_address(full_address):
"""parses the full_address and returns the parsed data as a dictionary
currently a stub that returns test data
"""
return {'contact_no': '(+22) 936107349', 'email': 'a@a.com', 'house': 'Ze Mansion', 'city': 'Dummy Data'}
p1 = PersonProfile.from_string("(+22) 936107349")
print(p1.contact_no)
print(p1.city)