任何判断对象是否在课堂上被启动的方法 [英] any ways to judge whether an object is initilized or not in a class

查看:38
本文介绍了任何判断对象是否在课堂上被启动的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很疑惑如何确定一个对象是否在一个类中启动,有人可以给我任何指示吗?

这里是一个示例代码:


级咖啡:

def boil(个体经营):

self.temp = 80


a = coffer()

如果a.temp 60:

打印它已煮沸


用C ++语言我们必须首先启动一个变量,所以没有这样的问题,但是在python中如果我们不调用a.boil(),我们就不会这样了。 b $ b让self.temp被启动,以确定在使用self.temp之前是否已启动



解决方案

momobear schrieb:


我很疑惑如何确定一个物品是否是
在一堂课中,任何人都可以给我任何指示吗?

这里有一个示例代码:


班级咖啡:

def boil(self):

self.temp = 80

a = coffer()

如果a.temp 60 :

print"它'煮沸了'


用C ++语言我们必须首先启动一个变量,所以没有

这样的问题,但在python中,如果我们不调用a.boil(),我们就不会让b $ b得到self.temp来启动,无论如何确定它是否已经启动

在使用self.temp之前。



你想要将boil称为__init__,这是python的构造函数名称。

然后它会在类似的语句中被调用


a =咖啡()


自动。

Diez

3月19日下午4:19,Diez B. Roggisch < d ... @nospam.web.dewrote:


momobear schrieb:


hi,我很困惑如何确定一个对象是否在一个类中启动,有人可以给我任何指示吗?

这里有一个示例代码:


class coffee:

def boil(self):

self.temp = 80


a = coffer()

如果a.temp 60:

print" it''s boiled"


在C ++语言中我们必须首先启动变量,所以没有

这样的问题,但在python中如果我们不要调用a.boil(),我们不会

让self.temp被启动,任何方式来确定它是否已经启动

之前自我.temp可以使用。



你想要将boil称为__init__,这是python'的构造函数名称。

然后它将在类似的语句中调用


a =咖啡()


自动。


Diez



抱歉,我应该添加更多代码来实现我的想法。

类咖啡:

def __init __(自我):

'''''

在这里做点什么

'''''

def boil(self):

self.temp = 80

a = coffer()

如果a.temp 60:

打印它煮沸了


" momobear" < wg **** @ gmail.comwrites:


class coffee:

def __init __(self):

'''''

在这里做点什么

''''''

def boil(self):

self.temp = 80

a = coffer()

如果a.temp 60:

打印它被煮沸了



class Coffee(对象):

def __init __(自我):

self.temp = 20 < br $> b $ b def boil(self):

self.temp = 80

a = coffee()

如果a.temp 60:

打印它已煮沸


在Python中,在TitleCase中命名类是常规的,
$ _ $ b $ lower_case实例。


最好继承另一个班级的每个班级,导致

单所有类和类型的层次结构。如果你不想要任何其他类的行为,那么''对象'就是你要选择的那个。

至于原始问题:__init__方法在构造函数之后立即调用一个类

,这样就可以初始化

任何实例属性。


-

\在我的柠檬水摊上,我曾经免费赠送第一杯玻璃,而第二杯则收5美元。替换物|

_o__)包含解毒剂。 - Emo Philips |
Ben Finney


hi, I am puzzled about how to determine whether an object is
initilized in one class, anyone could give me any instructions?
here is an example code:

class coffee:
def boil(self):
self.temp = 80

a = coffer()
if a.temp 60:
print "it''s boiled"

in C++ language we must initilized a variable first, so there is no
such problem, but in python if we don''t invoke a.boil(), we will not
get self.temp to be initilized, any way to determine if it''s initilzed
before self.temp be used.

解决方案

momobear schrieb:

hi, I am puzzled about how to determine whether an object is
initilized in one class, anyone could give me any instructions?
here is an example code:

class coffee:
def boil(self):
self.temp = 80

a = coffer()
if a.temp 60:
print "it''s boiled"

in C++ language we must initilized a variable first, so there is no
such problem, but in python if we don''t invoke a.boil(), we will not
get self.temp to be initilized, any way to determine if it''s initilzed
before self.temp be used.

You want boil to be called __init__, which is python''s constructor name.
Then it will be called in a statement like

a = coffee()

automatically.
Diez


On Mar 19, 4:19 pm, "Diez B. Roggisch" <d...@nospam.web.dewrote:

momobear schrieb:

hi, I am puzzled about how to determine whether an object is
initilized in one class, anyone could give me any instructions?
here is an example code:

class coffee:
def boil(self):
self.temp = 80

a = coffer()
if a.temp 60:
print "it''s boiled"

in C++ language we must initilized a variable first, so there is no
such problem, but in python if we don''t invoke a.boil(), we will not
get self.temp to be initilized, any way to determine if it''s initilzed
before self.temp be used.


You want boil to be called __init__, which is python''s constructor name.
Then it will be called in a statement like

a = coffee()

automatically.

Diez

sorry, I should add more code to implement my ideas.
class coffee:
def __init__(self):
''''''
do something here
''''''
def boil(self):
self.temp = 80

a = coffer()
if a.temp 60:
print "it''s boiled"


"momobear" <wg****@gmail.comwrites:

class coffee:
def __init__(self):
''''''
do something here
''''''
def boil(self):
self.temp = 80

a = coffer()
if a.temp 60:
print "it''s boiled"

class Coffee(object):
def __init__(self):
self.temp = 20
def boil(self):
self.temp = 80

a = coffee()
if a.temp 60:
print "it''s boiled"

In Python, it''s conventional to name classes in TitleCase, and
instances in lower_case.

It''s also best to inherit every class from another class, leading to a
single hierarchy for all classes and types. ''object'' is the one to
choose if you don''t want the behaviour of any other class.
As for the original question: the __init__ method of a class is called
immediately after the constructor, so that''s the place to initialise
any instance attributes.

--
\ "At my lemonade stand I used to give the first glass away free |
`\ and charge five dollars for the second glass. The refill |
_o__) contained the antidote." -- Emo Philips |
Ben Finney


这篇关于任何判断对象是否在课堂上被启动的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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