返回类的新实例的方法? [英] Method returning new instance of class?

查看:63
本文介绍了返回类的新实例的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有点受到装饰者讨论的启发,我正试图解决一些我一直在避免的事情。


基本上我正在努力创造一个类的非破坏性转换 - 是一种类型的实例 - 是一种方式。


我目前正在构思解决方案的方式,我是什么需要的是一个返回类的新实例的类的方法




我确定这不是新领域。


建议赞赏。


艺术

解决方案

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

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




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

copy.deepcopy。


问候,

Martin

"亚瑟" < AJ ****** @ optonline.com>写道:

我目前正在构思一个解决方案,我需要的是一个类的方法,它返回一个新的类实例。


< br $>
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



这篇关于返回类的新实例的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆