且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

返回类的新实例的方法?

更新时间:2023-12-02 19:42:04

Arthur写道:基本上我试图创建一个类的实例的非破坏性转换 - 是一种放置它的方法。

我目前的概念化方式一个解决方案,我需要的是一个返回类的新实例的类的方法。




所以你想要一个对象的副本。我会使用copy.copy,也许

copy.deepcopy。


问候,

Martin

"亚瑟" < AJ ****** @ optonline.com>写道:
我目前正在构思一个解决方案,我需要的是一个类的方法,它返回一个新的类实例。



class foo:

def bar(个体经营):

返回foo()


什么'这是问题吗?




" Martin v.L?wis" <毫安**** @ v.loewis.de>在消息中写道

news:41 *********************** @ news.freenet.de ...
Arthur写道:
基本上我试图创建一个类的实例的非破坏性转换 - 是一种放置它的方法。

我目前正在构思一个解决方案的方式,我需要的是一个返回类的新实例的类的
方法。
所以你想要一个对象的副本。我会使用copy.copy,也许
copy.deepcopy。




这是我的第一直觉。也许我的问题就在这里。


该应用程序是图形化的,我使用Boost

库(vpython,new)在C ++中使用Python扩展版)。我的类实例有一个属性,它是一个vpython对象的
。 Copy.copy并没有让我到达我需要的位置因为我的新

实例获得了对同一个vpython对象的引用,并且对它的更改是

反映在原始实例。 Copy.deepcopy不适用于更多

模糊的原因。我收到一条从vpython产生的错误消息,当我想要在新实例上更改对象的属性时 - 虽然我是

以相同的方式与它进行交互在

原始实例上执行时效果很好。


但是你知道为什么会这样吗?


如果这听起来完全不合逻辑,我会回去检查一下自己 - 因为

当然实际的常规比我更复杂

描述,我想我有可能从某个地方掉下来。

其他。


艺术
问候,
马丁



A bit inspired by the decorator discussions, I''m trying to tackle something
I had been avoiding.

Essentially I am trying to create a non-destructive tranformation of an
instance of a class - is one way of putting it.

The way I am currently conceptualizing a solution, what I need is a method
of the class that returns a new instance of the class.

I''m sure this is not new territory.

Suggestions appreciated.

Art

Arthur wrote:
Essentially I am trying to create a non-destructive tranformation of an
instance of a class - is one way of putting it.

The way I am currently conceptualizing a solution, what I need is a method
of the class that returns a new instance of the class.



So you want a copy of the object. I''d use copy.copy for this, perhaps
copy.deepcopy.

Regards,
Martin


"Arthur" <aj******@optonline.com> writes:
The way I am currently conceptualizing a solution, what I need is a method
of the class that returns a new instance of the class.



class foo:
def bar(self):
return foo()

What''s the problem?



"Martin v. L?wis" <ma****@v.loewis.de> wrote in message
news:41***********************@news.freenet.de...
Arthur wrote:
Essentially I am trying to create a non-destructive tranformation of an
instance of a class - is one way of putting it.

The way I am currently conceptualizing a solution, what I need is a method of the class that returns a new instance of the class.
So you want a copy of the object. I''d use copy.copy for this, perhaps
copy.deepcopy.



That was my first instinct. And perhaps my problem is in here somewhere.

The app is graphical, and I use a Python extensions in C++ using the Boost
library (vpython, new version). My class instance has an attribute which is
a vpython object. Copy.copy doesn''t get me where I need to be because my new
instance gets a reference to the same vpython object, and changes to it are
reflected in the original instance. Copy.deepcopy doesn''t work for more
obscure reasons. I get an error message generating up from vpython when I
try to change an attribute of the object on the new instance - though I am
interacting with it in the same manner that works fine when performed on the
original instance.

But do you see any reason why this might be?

If it sounds totally illogical, I''ll go back and check myself - because of
course the actual sitruation is a bit more complicated than what I am
describing, and I guess it is possible I am falling off the ledge somewhere
else.

Art
Regards,
Martin