在Python中初始化嵌套类 [英] Initialising nested classes in Python
问题描述
假设我要创建一个房屋"类,该类具有自己的某些属性,但还具有一个(嵌套的)居民"类,该类具有一些属性并具有一个强制属性姓".尽管没有任何居民,但房屋实例可能存在.如何创建它,以便我最终可以执行以下操作?
myhouse = House()residentX = myhouse.resident('Smith')
当前,我将其设置为嵌套类,但由于初始化嵌套类(此时不一定需要),我在尝试初始化myhouse时遇到麻烦
class House:def __init __():self.someattribute =< someattribute>self.resident = self.Resident()班级居民:def __init __(自己,姓):self.surname =姓
我知道我可以重组代码以不使用嵌套类,然后在代码中将任何居民明确地绑定到房屋.但是,我想在此处使用点符号(myhouse.resident)将居民自动绑定到房屋.
此外,我了解到python中的嵌套类有些皱眉-我愿意就如何以更pythonic的方式进行上述操作提出建议.
我将分解 Resident
类,并为 .resident
赞:
class House:def __init __():self.someattribute =< someattribute>self._resident =无@财产def居民(自己):返回self._resident@ resident.setterdef居民(个人,姓):r =居民(姓)self._resident = r班级居民:def __init __(自己,姓):self.surname =姓
但是,如果希望 .resident
是可调用的,但又想跟踪房屋的居民,则仍然可以拆分
class House:def __init __():self.someattribute =< someattribute>self.residents = []def居民(个人,姓):'''将居民添加到房屋中'''r =居民(姓)self.residents.append(r)返回r班级居民:def __init __(自己,姓):self.surname =姓
Let's say I want to create a class 'House' that has some attributes of its own, but also has a (nested?) 'Resident' class which has some attributes and has a mandatory attribute 'surname'. A house instance may exist though without any residents. How can create this so that I can eventually do the following?
myhouse = House()
residentX = myhouse.resident('Smith')
Currently I set this up as a nested class but run into trouble when I try and initialise myhouse given that it is requiring a surname at this point for the nested Resident class (which I don't necessarily have at this point)
class House:
def __init__(self):
self.someattribute = <someattribute>
self.resident = self.Resident()
class Resident:
def __init__(self, surname):
self.surname = surname
I know I can restructure the code to not use nested classes and then explicitly tie any resident to a house in my code. However, I would like to use the dot notation here (myhouse.resident) to automatically tie a resident to a house.
Also, I understand that nested classes in python are somewhat frowned upon - I'm open to suggestions on how to do the above in a more pythonic manner.
I would break out the Resident
class and use a property/setter for .resident
Like this:
class House:
def __init__(self):
self.someattribute = <someattribute>
self._resident = None
@property
def resident(self):
return self._resident
@resident.setter
def resident(self, surname):
r = Resident(surname)
self._resident = r
class Resident:
def __init__(self, surname):
self.surname = surname
However, if you want .resident
to be callable but also want to track the house's residents, you can still break out the Resident
class, and use:
class House:
def __init__(self):
self.someattribute = <someattribute>
self.residents = []
def resident(self, surname):
'''
Add a resident to the house
'''
r = Resident(surname)
self.residents.append(r)
return r
class Resident:
def __init__(self, surname):
self.surname = surname
这篇关于在Python中初始化嵌套类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!