更多__init__方法 [英] More __init__ methods

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

问题描述



我知道只有一种__init__方法(至少,我认为)。


我经常需要创建一个对象以不同的方式,例如

传递一个字符串作为参数,或一个整数,或另一个对象。要实现这个
,我将参数的默认值设置为None然后

我在__init__中有一些if ... elif。


这是一个好习惯吗?它实际上是有效的,但有时我认为

这样__init__方法可能会变得太复杂,对于

示例当一个对象可以使用多个参数创建时

不同的组合。


希望我的疑问很清楚。

Hi,
I know there can be only one __init__ method (at least, I think).

Often I need an object to be created in different ways, for example
passing a string as argument, or an integer, or another object. To
achieve this I put the default value of the arguments to None and then
I some if...elif inside the __init__.

Is this a good practice? It actually works, but sometimes I think that
in this way the __init__ method can become too complicated, for
example when an object can be created using more than one argument and
in different combinations.

Hope my doubt is clear.

推荐答案

On Thu,2008年11月6日16:49:08 +0100,Mr.SpOOn写道:
On Thu, 06 Nov 2008 16:49:08 +0100, Mr.SpOOn wrote:

我知道只有一种__init__方法(至少,我我认为。通常我需要以不同的方式创建一个对象,例如

传递一个字符串作为参数,或一个整数,或另一个对象。为了实现这一点,我将参数的默认值设置为None,然后我将

一些if _ elif在__init__中。


这是一个好习惯吗?它实际上是有效的,但有时我认为这样__init__方法可能会变得太复杂,例如

当一个对象可以使用多个参数创建时在

不同的组合。
I know there can be only one __init__ method (at least, I think).

Often I need an object to be created in different ways, for example
passing a string as argument, or an integer, or another object. To
achieve this I put the default value of the arguments to None and then I
some if...elif inside the __init__.

Is this a good practice? It actually works, but sometimes I think that
in this way the __init__ method can become too complicated, for example
when an object can be created using more than one argument and in
different combinations.



我通常使用`classmethod()`\s作为替代构造函数然后。

例如:


A类(对象):

def __init __(self,a,b,c) :

self.a = a

#...

@classmethod

def from_string (cls,s):

#...

返回cls(a,b,c)


Ciao,< br>
Marc''BlackJack''Rintsch

I''m usually using `classmethod()`\s as alternative "constructors" then.
For example:

class A(object):
def __init__(self, a, b, c):
self.a = a
# ...

@classmethod
def from_string(cls, s):
# ...
return cls(a, b, c)

Ciao,
Marc ''BlackJack'' Rintsch


2008年11月6日星期四下午4:59,Marc''BlackJack''Rintsch< ; bj **** @ gmx.netwrote:
On Thu, Nov 6, 2008 at 4:59 PM, Marc ''BlackJack'' Rintsch <bj****@gmx.netwrote:

class A(object):

def __init __(self,a,b, c):

self.a = a

#...


@classmethod

def from_string(cls,s):

#...

返回cls(a,b,c)
class A(object):
def __init__(self, a, b, c):
self.a = a
# ...

@classmethod
def from_string(cls, s):
# ...
return cls(a, b, c)



谢谢。

我认为是时候学习装饰了。

Thanks.
I think it''s time to study decorators.


Mr.SpOOn写道:
Mr.SpOOn wrote:

2008年11月6日星期四下午4:59,Marc''BlackJack' 'Rintsch< bj **** @ gmx.netwrote:
On Thu, Nov 6, 2008 at 4:59 PM, Marc ''BlackJack'' Rintsch <bj****@gmx.netwrote:

> A类(对象):
def __init __(self,a,b ,c):
self.a = a
#...
@classmethod
def from_string(cls,s):
#.. 。
返回cls(a,b,c)
>class A(object):
def __init__(self, a, b, c):
self.a = a
# ...

@classmethod
def from_string(cls, s):
# ...
return cls(a, b, c)



谢谢。

我认为是时候学习装饰师了。


Thanks.
I think it''s time to study decorators.



虽然这不是坏事,但你真的不需要做什么只需了解这些例子就可以了:b $ b ''只是

说做你需要的任何东西来制作这些方法

类方法,而不是实例方法。


TJG


While that''s no bad thing, you don''t really need to do
that simply to understand these examples: they''re just
saying "do whatever you need to to make these method
class methods, not instance methods".

TJG


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

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