gpt4 book ai didi

python - 惯用地将 BaseClass 对象转换为 SubClass 对象?

转载 作者:太空狗 更新时间:2023-10-29 20:23:01 24 4
gpt4 key购买 nike

有一个基类Base和一个子类Special

class Base(object):
def __init__(self, name):
self.name = name
def greet(self):
return 'Hello %s' % self.name

class Special(Base):
def __init__(self, name):
super(Special, self).__init__(name)
def rhyme(self):
return 'Hi %s! How are you? Fine, thanks. What about you?' % self.name

如何将 Base 实例转换为 Special 实例?目前,我在 Special 上定义了一个 classmethod,它只是重新分配了 __dict__:

class Special(Base):
...
@classmethod
def from_base(cls, baseobj):
special = Special()
special.__dict__ = baseobj.__dict__
return special

这是惯用语吗?如果不是会是什么?

附言示例场景:基类是一些默认实现。在野外,您可能会找到基类的对象。现在在一些项目中,基类已经是子类,并且在子类中添加了特殊的方法。现在你大部分时间仍然使用基类对象,但有时你会想“升级”到特殊类,因为你需要访问一些方法。

最佳答案

您可以通过定义替代构造函数并重新分配实例的 __class__ 属性来实现这一点。

class Base(object):
def __init__(self, name):
self.name = name

def greet(self):
return 'Hello %s' % self.name

@classmethod
def alt_constructor(cls, *args, **kwargs):
obj = cls(*args, **kwargs)
obj.__class__ = Special
return obj


class Special(Base):
def __init__(self, name):
super(Special, self).__init__(name)

def rhyme(self):
return 'Hi %s! How are you? Fine, thanks. What about you?' % self.name


>>> s = Base.alt_constructor("test")
>>> print s.rhyme()
Hi test! How are you? Fine, thanks. What about you?

编辑:

将构造函数从 Special 移至 Base

如果您不能修改 Base 类,您可以向 Special 添加一个类方法,这将更改传递给它的任何对象的类。

class Base(object):
def __init__(self, name):
self.name = name

def greet(self):
return 'Hello %s' % self.name


class Special(Base):
def __init__(self, name):
super(Special, self).__init__(name)

def rhyme(self):
return 'Hi %s! How are you? Fine, thanks. What about you?' % self.name

@classmethod
def convert_to_special(cls, obj):
obj.__class__ = Special

>>> b = Base("test")
>>> print type(b)
<class '__main__.Base'>

>>> Special.convert_to_special(b)
>>> print type(b)
<class '__main__.Special'>

一个更通用的解决方案是创建一个可以添加到任何类的 mixin。

class ConverterMixin(object):

@classmethod
def convert_to_class(cls, obj):
obj.__class__ = cls


class Special(ConverterMixin, Base):
def __init__(self, name):
super(Special, self).__init__(name)

def rhyme(self):
return 'Hi %s! How are you? Fine, thanks. What about you?' % self.name

>>> b = Base("test")
>>> print type(b)
<class '__main__.Base'>

>>> Special.convert_to_class(b)
>>> print type(b)
<class '__main__.Special'>

关于python - 惯用地将 BaseClass 对象转换为 SubClass 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18020074/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com