简体   繁体   English

如何命名__init__用于确定新对象的值的方法?

[英]How to name method used by __init__ to determine value of new object?

I'm new to Python, I started learning Python3.3 few days ago. 我是Python的新手,几天前我开始学习Python3.3。

Let's have 2 classes, Source and Dest ; 让我们有两个类, SourceDest Dest is declared before Source . DestSource之前声明。

I'd like to make a object belonging to class Dest with content according to on object belonging to class Source . 我想根据属于Source类的on对象来使一个属于Dest类的对象具有内容。

If it was C++ (which I know quite well), to do thas I could allow casting Source to Dest by writing Source::operator Dest() , operator Dest(Source) , Dest::Dest(Source) , operator Dest(Source&) or Dest::Dest(Source&) . 如果是C ++(我知道得很好),做全髋关节置换,我可以让铸造SourceDest通过编写Source::operator Dest() operator Dest(Source)Dest::Dest(Source)operator Dest(Source&)Dest::Dest(Source&)

I think about decorating Dest 's __init__ to set Dest 's object's value on value returned by passed argument's special_method (if exist), which should return object with attributes similar to Dest 's object. 我考虑装饰Dest__init__以将Dest的对象的值设置为传递的参数的special_method (如果存在)返回的值,该参数应返回具有与Dest的对象相似的属性的对象。

In code it could look like that: 在代码中可能看起来像这样:

def allow_conversion(prev_init, special_method_name):
    def wrap(self, source, *args, **dic):
        try:
            #call getattr(source, special_method_name)(*args, **dic)
            #copy attributes of returned object (don't know how to implement)
        except Exception: #if getattr(source, special_method_name)
            pass          #doesn't exist or raised Exception
        finally:
            prev_init(source, *args, **dic) #original __init__ is always called
    return wrap

class Dest:
    @allow_conversion('special_method')
    def __init__(self):
        pass #do some initializing

I wonder how to name the special_method . 我不知道如何命名special_method Is there any habit? 有什么习惯吗?

If I understand your question correctly, you simply want to create an instance of Dest using an instance of Source . 如果我正确理解了您的问题,则只想使用Source的实例创建Dest的实例。 For that, you should define a class method in Dest which takes an instance of Source as an argument, and returns a properly initialized instance of Dest . 为此,您应该在Dest定义一个类方法,该方法将Source的实例作为参数,并返回正确初始化的Dest实例。

class Source:
    def __init__(self, x, y):
        self.x = x
        self.y = y

class Dest:
    def __init__(self, a):
        self.a = a

    @classmethod
    def from_source(cls, src):
        return Dest(src.x + src.y)


s = Source(5, 3)
d = Dest.from_source(s)
assert d.a == 8

All the logic for "converting" an instance of Source to an instance of Dest will reside in Dest.from_source . 用于将Source实例“转换”为Dest实例的所有逻辑都将驻留在Dest.from_source

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 object 和 __init__ 方法 - object and __init__ method 如何通过调用__init__方法中的方法在python中引入对象的属性? - How to introduce the attributes of an object in python by calling a method in __init__ method? zmq PUBhandler不能在__init __()方法中使用 - zmq PUBhandler could not be used in __init__() method 如何对字符串进行split()并将其传递给对象的__init __()方法? - How to to split() a string and pass it into an object's __init__() method? 如何使用具有 __init__ 方法的子类创建一个空的 object? - How to create an empty object with child classes that have __init__ method? 如何在python的__init__方法中覆盖默认值? - How should I override the default value in __init__ method in python? 类和对象上的__new__和__init__ - `__new__` and `__init__` on class and object 使用 __init__ 方法更新对象的实例 - Updating an instance of an object with the __init__ method 在类X的__new__方法中取消类型X的对象在返回未拾取的对象时调用__init__,为什么? - Unpickling object of type X in the __new__ method of class X calls __init__ on returning the unpickled object, why? 创建新对象并调用方法时,显示错误__init __()恰好接受6个参数(给定1个) - When creating a new object and calling a method show error __init__() takes exactly 6 arguments (1 given)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM