如何创建仅继承特定属性的对象的别名
How can I create aliases of objects that inherit only a particular attribute
我正在 python 中制作一个程序,用户可以在其中将姓名和号码添加到列表中。
我将条目保存在具有 2 个属性的 class 中,并将 class 的实例保存为列表中的对象:
class NewEntry:
def __init__(self):
self.name = ""
self.number = ""
entry = NewEntry()
entry.name = userinput[1]
entry.number = userinput[2]
entrylist.append(entry)
我现在正在尝试添加一个名为 alias 的函数,它将以新名称保存一个条目。别名应该以这样一种方式连接到原始条目,即更改其中一个的编号会将每个相应别名的编号更改为相同的值。
这是我尝试这样做的代码:
for key in entrylist:
if key.name == userinput[1]:
entry = key
entry.name = userinput[2]
entrylist.append(entry)
这没有按预期工作。创建新别名时,它还会更改键的 .name,我试图仅从中继承数字属性。
我也尝试过以下方法:
for key in entrylist:
if key.name == userinput[1]:
entry = NewEntry()
entry.name = userinput[2]
entry.number = key.number
entrylist.append(entry)
长话短说:
我希望能够创建对象的副本,其中 .number-attribute 在所有别名中都相同。我希望所有这些对象的名称都不同,但数量保持不变。改变其中一个别名的数量也会影响其他别名。
您可以 link 通过添加 alias_of
属性来表示名称是什么的别名。
如果您将它与 number
属性的 property
配对,那么您可以控制实际设置的对象。
class NewEntry:
# I added the attributes as constructor
# arguments since you're setting them anyway
def __init__(self, name='', number=None, alias_of=None):
self.name = name
# since we want self.number to be a property
# it is common practice to use a _ to signify
# that an attribute is private
self._number = number
# here alias will refer to the instance it's
# an alias of. It defaults to None
# or not an alias
self.alias_of = alias_of
# giving a method a @property decorator
# makes it get the attribute from here
# That way you control which number it's getting
@property
def number(self):
if self.alias_of:
return self.alias_of.number
else:
return self._number
# The same is true if you give it a setter
# method. It controls which instances number
# that you set
@number.setter
def number(self, value):
if self.alias_of:
self.alias_of.number = value
else:
self._number = value
entry = NewEntry('my name', number=1)
entry2 = NewEntry('my name 2', number=2)
# here we say that entry3 is an alias of entry
entry3 = NewEntry('my other name', alias_of=entry)
print(entry.number)
print(entry2.number)
# and here entry3 is actually setting entry
# 1's number attribute
entry3.number = 3
print(entry.number)
我正在 python 中制作一个程序,用户可以在其中将姓名和号码添加到列表中。 我将条目保存在具有 2 个属性的 class 中,并将 class 的实例保存为列表中的对象:
class NewEntry:
def __init__(self):
self.name = ""
self.number = ""
entry = NewEntry()
entry.name = userinput[1]
entry.number = userinput[2]
entrylist.append(entry)
我现在正在尝试添加一个名为 alias 的函数,它将以新名称保存一个条目。别名应该以这样一种方式连接到原始条目,即更改其中一个的编号会将每个相应别名的编号更改为相同的值。 这是我尝试这样做的代码:
for key in entrylist:
if key.name == userinput[1]:
entry = key
entry.name = userinput[2]
entrylist.append(entry)
这没有按预期工作。创建新别名时,它还会更改键的 .name,我试图仅从中继承数字属性。
我也尝试过以下方法:
for key in entrylist:
if key.name == userinput[1]:
entry = NewEntry()
entry.name = userinput[2]
entry.number = key.number
entrylist.append(entry)
长话短说: 我希望能够创建对象的副本,其中 .number-attribute 在所有别名中都相同。我希望所有这些对象的名称都不同,但数量保持不变。改变其中一个别名的数量也会影响其他别名。
您可以 link 通过添加 alias_of
属性来表示名称是什么的别名。
如果您将它与 number
属性的 property
配对,那么您可以控制实际设置的对象。
class NewEntry:
# I added the attributes as constructor
# arguments since you're setting them anyway
def __init__(self, name='', number=None, alias_of=None):
self.name = name
# since we want self.number to be a property
# it is common practice to use a _ to signify
# that an attribute is private
self._number = number
# here alias will refer to the instance it's
# an alias of. It defaults to None
# or not an alias
self.alias_of = alias_of
# giving a method a @property decorator
# makes it get the attribute from here
# That way you control which number it's getting
@property
def number(self):
if self.alias_of:
return self.alias_of.number
else:
return self._number
# The same is true if you give it a setter
# method. It controls which instances number
# that you set
@number.setter
def number(self, value):
if self.alias_of:
self.alias_of.number = value
else:
self._number = value
entry = NewEntry('my name', number=1)
entry2 = NewEntry('my name 2', number=2)
# here we say that entry3 is an alias of entry
entry3 = NewEntry('my other name', alias_of=entry)
print(entry.number)
print(entry2.number)
# and here entry3 is actually setting entry
# 1's number attribute
entry3.number = 3
print(entry.number)