新的类实例? [英] New inited instance of class?

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

问题描述

是否有内置的方法来制作自己的另一个实例

类?我真的希望类型(自我)(* args,**关键字)以这种方式工作。

目前我正在这样做:


def new (self,* args,**关键字):

来自新的导入实例

s = instance(self .__ class__)

if hasattr(D (),''__ init __''):

s .__ init __(* args,**关键字)

返回s


-

你必须给他留下深刻的印象!独立而且勇敢,但经常会做出蠢蠢欲动的东西!

--50s Diana Dane到90s Diana Dane

Is there a builtin way of making making another instance of your own
class? I really expected type(self)(*args, **keywords) to work this way.
Currently i''m doing this:

def new(self,*args,**keywords):
from new import instance
s=instance(self.__class__)
if hasattr(D(),''__init__''):
s.__init__(*args,**keywords)
return s

--
"You have to impress him! Be independent and plucky, but often do
things that are moronic and out of character!"
--50s Diana Dane to 90s Diana Dane

推荐答案

Samuel Kleiner< sa*@samuel-kleiners-computer.local>写道:
Samuel Kleiner <sa*@samuel-kleiners-computer.local> writes:
是否有内置的方法来制作自己的类的另一个实例?我真的希望类型(自我)(* args,**关键字)以这种方式工作。
Is there a builtin way of making making another instance of your own
class? I really expected type(self)(*args, **keywords) to work this way.




不自我.__ class __(* args,** keywords )工作?


|> oug



Doesn''t self.__class__(*args, **keywords) work?

|>oug




Samuel Kleiner在留言中写道......

Samuel Kleiner wrote in message ...
是否有内置的方法来制作自己的类的另一个实例?


你的意思是,从内部(来自该类的一个实例方法)?


def new(self,* args, ** kargs):

返回self .__ class __(* args,** kargs)

我真的希望类型(自我)(* args,**关键字)能够工作这条路。


适合我。它会给你什么回溯?

目前我正在这样做:

def new(self,* args,** keywords):
来自new导入实例
s = instance(self .__ class__)
如果hasattr(D(),''__ init __''):
s .__ init __(* args,** keywords)
return s
Is there a builtin way of making making another instance of your own
class?
You mean, from the inside (from one of the instance methods of the class)?

def new(self, *args, **kargs):
return self.__class__(*args, **kargs)
I really expected type(self)(*args, **keywords) to work this way.
Works for me. What traceback does it give you?
Currently i''m doing this:

def new(self,*args,**keywords):
from new import instance
s=instance(self.__class__)
if hasattr(D(),''__init__''):
s.__init__(*args,**keywords)
return s




这很难看。


-

Francis Avila



That''s ugly.

--
Francis Avila


Francis Avila写道:
Francis Avila wrote:

Samuel Kleiner在留言中写道......

Samuel Kleiner wrote in message ...
是否内置制作你自己的另一个实例的方法?
你的意思是,从内部(来自该类的一个实例方法)?
Is there a builtin way of making making another instance of your own
class?
You mean, from the inside (from one of the instance methods of the class)?




是的。

def new(self,* args,** kargs):
返回self .__ class __(* args,** kargs)



Yes.
def new(self, *args, **kargs):
return self.__class__(*args, **kargs)




这很有效。谢谢。



This works. Thanks.

我真的希望类型(自我)(* args,**关键字)以这种方式工作。
I really expected type(self)(*args, **keywords) to work this way.



适合我。它会给你什么回溯?



Works for me. What traceback does it give you?




不适合我。我真的想把它称为函数本身,并且


类型(self)(argument1,argument2,argument3)


失败<回扣(最近一次调用最后一次):

文件"< stdin>",139行,在?

文件"< stdin>",第78行,在__add__中

TypeError:instance()最多需要2个参数(3个给定)





self .__ class __(argument1,argument2,argument3)





Not for me. I really want to call it as the function itself, and

type(self)(argument1,argument2,argument3)

fails with

Traceback (most recent call last):
File "<stdin>", line 139, in ?
File "<stdin>", line 78, in __add__
TypeError: instance() takes at most 2 arguments (3 given)

whereas

self.__class__(argument1,argument2,argument3)

does not

目前我正在这样做:

[我的代码]
Currently i''m doing this:

[my code]


这很难看。


That''s ugly.




是的。


-

然而,令人鼓舞的是,我发现在整个源代码中,

极其保守的编码实践和良好的错误检查无处不在

意味着我们的软件在处理IPv6地址时不会崩溃。

--Joe Loughry,Lockh eed Martin Space and Strategic Missiles,RADIANT MERCURY



Yes.

--
On an encouraging note, however, I found that throughout the source code,
extremely conservative coding practices and good error checking everywhere
means that our software does not crash when handling IPv6 addresses.
--Joe Loughry, Lockheed Martin Space and Strategic Missiles, RADIANT MERCURY


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

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