我希望物件在它們的例程中呼叫某個方法(在變數中分配的特定方法)。我設法用 dict 來做到這一點,我還設法通過在創建物件后分配變數來做到這一點。哪種方式更好?還有其他更清潔的方法嗎?
第一種作業方式(字典):
class Foo:
def __init__(self, name, funcin) -> None:
self.name = name
self.func = {
'bear': self.bear,
'boar': self.boar,
}[funcin]
def routine(self):
print('fluff')
self.func()
def bear(self):
print(f'I, {self.name}, am a bear and I like honey.')
def boar(self):
print(f'I, {self.name}, am a boar and I like fruits.')
bob = Foo('bob', 'bear')
mark = Foo('mark', 'boar')
bob.routine()
mark.routine()
第二種作業方式(另一行分配):
class Foo:
def __init__(self, name) -> None:
self.name = name
def routine(self):
print('fluff')
self.func()
def bear(self):
print(f'I, {self.name}, am a bear and I like honey.')
def boar(self):
print(f'I, {self.name}, am a boar and I like fruits.')
Kona = Foo('Kona')
Kona.func = Kona.bear
John = Foo('John')
John.func = John.boar
Kona.routine()
John.routine()
無效的方法:
# NameError: name 'bear' is not defined
class Foo:
def __init__(self, name, funcin) -> None:
self.name = name
self.func = self.funcin
def bear(self):
print(f'I, {self.name}, am a bear and I like honey.')
bob = Foo('bob', bear)
bob.func()
# NameError: name 'self' is not defined
class Foo:
def __init__(self, name, funcin) -> None:
self.name = name
self.func = funcin
def bear(self):
print(f'I, {self.name}, am a bear and I like honey.')
bob = Foo('bob', self.bear)
bob.func()
# NameError: name 'Kim' is not defined
class Foo:
def __init__(self, name, funcin) -> None:
self.name = name
self.func = funcin
def bear(self):
print(f'I, {self.name}, am a bear and I like honey.')
Kim = Foo('Kim', Kim.bear)
Kim.func()
# TypeError: Foo.bear() missing 1 required positional argument: 'self'
class Foo:
def __init__(self, name, funcin) -> None:
self.name = name
self.func = funcin
def bear(self):
print(f'I, {self.name}, am a bear and I like honey.')
bob = Foo('bob', Foo.bear)
bob.func()
uj5u.com熱心網友回復:
類的方法就像它們通過類的__dict__屬性在字典中一樣可用。所以在你提出的第一個解決方案中,你也可以self.func這樣定義:
self.func = Foo.__dict__[funcin].__get__(self)
因此,通過__dict__[funcin]我們從類物件中檢索方法,__get__我們得到一個具有正確self系結的函式。
為了使它更通用,以便您不必提及Foo:
self.func = self.__class__.__dict__[funcin].__get__(self)
uj5u.com熱心網友回復:
一個常見的解決方案是類繼承。從抽象基類到 mixin 有多種選擇,但這里是一個簡單的開始
class Foo:
def __init__(self, name) -> None:
self.name = name
def func(self):
raise NotImplementedError
def routine(self):
print('fluff')
self.func()
class Bear(Foo):
def func(self):
print(f'I, {self.name}, am a bear and I like honey.')
class Boar(Foo):
def func(self):
print(f'I, {self.name}, am a boar and I like fruits.')
bob = Bear('bob')
mark = Boar('mark')
bob.routine()
mark.routine()
uj5u.com熱心網友回復:
您可以使用 getattr():
class Foo:
def __init__(self, name, funcin) -> None:
self.name = name
self.func = getattr(self,funcin)
def routine(self):
print('fluff')
self.func()
def bear(self):
print(f'I, {self.name}, am a bear and I like honey.')
def boar(self):
print(f'I, {self.name}, am a boar and I like fruits.')
如果您不需要執行額外的(常見)絨毛,您可以簡單地分配self.routine = getattr(self,funcin)甚至不定義routine() 函式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/318004.html
下一篇:XML到物件映射
