请__init__解释 [英] __init__ explanation please

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

问题描述

我是Python和OOP的新手。我已经阅读了Mark Lutz的大部分书籍以及更多

在线并且可以编写简单的模块,但是当__init__

需要时我仍然无法获得用于而不是通过赋值创建类实例。对于

一些奇怪的原因,文献似乎认为这是理所当然的。我会

感谢任何有助于澄清这一点的指针或链接。


谢谢

I''m new to Python, and OOP. I''ve read most of Mark Lutz''s book and more
online and can write simple modules, but I still don''t get when __init__
needs to be used as opposed to creating a class instance by assignment. For
some strange reason the literature seems to take this for granted. I''d
appreciate any pointers or links that can help clarify this.

Thanks

推荐答案

我是Python和OOP的新手。我已经阅读了Mark Lutz的大部分书籍和更多
I''m new to Python, and OOP. I''ve read most of Mark Lutz''s book and more

online,可以编写简单的模块,但是当__init __ 而不是通过赋值创建类实例。对于

一些奇怪的原因,文献似乎认为这是理所当然的。我会

感谢任何有助于澄清这一点的指针或链接。
online and can write simple modules, but I still don''t get when __init__
needs to be used as opposed to creating a class instance by assignment. For
some strange reason the literature seems to take this for granted. I''d
appreciate any pointers or links that can help clarify this.



我不确定我是否理解你的问题但也许这会有所帮助:


如果你想要代码要在创建班级实例时运行,你需要使用__init__
。最常见的例子包括设置

实例的属性并进行一些检查,例如


class Person:

def __init __(self ,第一个,最后一个):

如果len(第一个)50或len(最后)50:

引发异常(''名字太长了。'')

self.first =第一次

self.last = last


你会像这样使用你的班级,


p1 =人(''John'',''史密斯'')

p2 =人(你不想要的一些长假名

除外,我不知道它是否真的长于50但是我们假设它是

是,史密斯)

#这最后一个会引发异常,所以你知道有些事情不合适


HTH,

Daniel

I''m not sure if I understand your question correctly but maybe this will help:

If you want code to be run upon creating an instance of your class you
would use __init__. Most common examples include setting attributes on
the instance and doing some checks, e.g.

class Person:
def __init__( self, first, last ):
if len( first ) 50 or len( last ) 50:
raise Exception( ''The names are too long.'' )
self.first = first
self.last = last

And you would use your class like so,

p1 = Person( ''John'', ''Smith'' )
p2 = Person( "Some long fake name that you don''t really want to
except, I don''t know if it''s really longer than 50 but let''s assume it
is", "Smith" )
# This last one would raise an exception so you know that something is not okay

HTH,
Daniel


Erik Lind写道:
Erik Lind wrote:

我是Python和OOP的新手。我已经阅读了Mark Lutz的大部分书籍以及更多

在线并且可以编写简单的模块,但是当__init__

需要时我仍然无法获得用于而不是通过赋值创建类实例。对于

一些奇怪的原因,文献似乎认为这是理所当然的。我会

感谢任何有助于澄清这一点的指针或链接。


谢谢

I''m new to Python, and OOP. I''ve read most of Mark Lutz''s book and more
online and can write simple modules, but I still don''t get when __init__
needs to be used as opposed to creating a class instance by assignment. For
some strange reason the literature seems to take this for granted. I''d
appreciate any pointers or links that can help clarify this.

Thanks



如果

__new__与new一起使用,你不一定需要__init__上课。


Colin W.

You don''t always need __init__ if
__new__ is used with a "new" class.

Colin W.


请继续讨论清单......
Please keep discussion on the list......

我不确定我是否理解你的问题,但也许这将是

帮助:


如果您希望在创建类的实例时运行代码,则

将使用__init__。最常见的例子包括设置

实例的属性并进行一些检查,例如


class Person:

def __init __(self ,第一个,最后一个):

如果len(第一个)50或len(最后)50:

引发异常(''名字太长了。'')

self.first =第一次

self.last = last


你会像这样使用你的班级,


p1 =人(''John'',''史密斯'')

p2 =人(你不想要的一些长假名

除外,我不知道它是否真的长于50但是我们假设它是

是,史密斯)

#这最后一个会引发异常,所以你知道有些东西不是

好​​吧


HTH,

Daniel
I''m not sure if I understand your question correctly but maybe this will
help:

If you want code to be run upon creating an instance of your class you
would use __init__. Most common examples include setting attributes on
the instance and doing some checks, e.g.

class Person:
def __init__( self, first, last ):
if len( first ) 50 or len( last ) 50:
raise Exception( ''The names are too long.'' )
self.first = first
self.last = last

And you would use your class like so,

p1 = Person( ''John'', ''Smith'' )
p2 = Person( "Some long fake name that you don''t really want to
except, I don''t know if it''s really longer than 50 but let''s assume it
is", "Smith" )
# This last one would raise an exception so you know that something is not
okay

HTH,
Daniel



当我通过assignement在某处创建实例时,代码是不是运行了se?


我认为人们可能想立即检查潜在的异常

,但文献中的大多数例子并没有这样做,不要
似乎做了一些在创建实例时无法完成的事情

稍后通过赋值。我错过了一些基本的东西。


Is not the code run when I create an instance by assignement somewhere else?

I take the point that one might want to check for potential exceptions
immediately, but most examples in the literature aren''t doing that and don''t
seem to be doing anything that would not be done when creating an instance
by assignment later somewhere. I''m missing something basic here.



你是什么意思在其他地方通过asignment创建一个实例?

What do you mean by "create an instance by asignment somewhere else"?


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

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