怎么用bool [英] how to use bool

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

问题描述

我有一些代码,我设置了一个bool类型变量,如果值

为false,我想从错误消息的方法返回..

是初学者我想在这里帮忙


class myclass:

.........

def mymethod(self):

success = True

msg ="所有验证确定>

success = validateSthing()

if(success == False):

msg =" sthing failed"

return(success,msg)


dosomeprocessing()

.....

success = validateSthingelse()

if(success == False):

msg =" sthingelse失败>

返回(成功,消息)

domoreprocessing()

... 。

返回(成功,消息)


i想知道这种做法是否正常..我需要

多种验证方式..这里有更好的方法吗?

这个?


谢谢

i have some code where i set a bool type variable and if the value
is false i would like to return from the method with an error msg..
being a beginner I wd like some help here

class myclass:
.........
def mymethod(self):
success=True
msg="all validation OK"
success=validateSthing()
if(success==False):
msg="sthing failed"
return (success,msg)

dosomeprocessing()
.....
success=validateSthingelse()
if(success==False):
msg="sthingelse failed"
return (success,msg)
domoreprocessing()
....
return(success,msg)

i would like to know if this way of doing this is OK..I have need of
many kinds of validations in this ..is there a better way of doing
this ?

thank you

推荐答案

ji ****** ***@gmail.com 写道:
ji*********@gmail.com wrote:

我有一些代码,我设置一个bool类型变量,如果值为

是假的我想从错误消息的方法返回..

作为初学者我想在这里帮忙


class myclass:

.........

def mymethod(self):

success = True

msg ="所有验证确定"

success = validateSthing()

if(success == False):

msg =" ; sthing失败

返回(成功,消息)

dosomeprocessing()

.....

success = validateSthingelse()

if(success == False):

msg =" sthingelse失败了

返回(成功,消息)

domoreprocessing()

....

返回(成功,msg)


i想知道这样做的方法是否正常..我需要

这个中的多种验证..有一个更好的方式来做这个吗?
i have some code where i set a bool type variable and if the value
is false i would like to return from the method with an error msg..
being a beginner I wd like some help here

class myclass:
.........
def mymethod(self):
success=True
msg="all validation OK"
success=validateSthing()
if(success==False):
msg="sthing failed"
return (success,msg)

dosomeprocessing()
.....
success=validateSthingelse()
if(success==False):
msg="sthingelse failed"
return (success,msg)
domoreprocessing()
....
return(success,msg)

i would like to know if this way of doing this is OK..I have need of
many kinds of validations in this ..is there a better way of doing
this ?



来测试布尔值,通常最好使用plain" if"或if

not声明:


如果成功:

...在这里处理成功...


如果不成功:

...在这里处理失败...


报告失败,使用例外(加注和尝试/除了

语句)。有关此主题的更多信息,请参阅教程。


< / F>

to test boolean values, it''s usually better to use plain "if" or "if
not" statements:

if success:
... handle success here ...

if not success:
... handle failure here ...

to report failures, use exceptions (the raise and try/except
statements). see the tutorial for more on this topic.

</F>


ji ********* @ gmail.com 写道:

我有一些代码,我设置了一个bool类型变量,如果值

为false,我想从错误消息的方法返回..

是初学者我想在这里帮忙


班级myclass:

.........

def mymethod(self):

success = True

msg =" all validation OK"

success = validateSthing()

if(success == False):

msg =" sthing failed"

return(success,msg)

>
dosomeprocessing()

.....

success = validateSthingelse()

if(success == Fal) se):

msg =" sthingelse failed"

return(成功,msg)

domoreprocessing()

....

返回(成功,消息)


i想知道这种做法是否正常..我需要

多种验证方式..这有更好的方法吗?

这个?
i have some code where i set a bool type variable and if the value
is false i would like to return from the method with an error msg..
being a beginner I wd like some help here

class myclass:
.........
def mymethod(self):
success=True
msg="all validation OK"
success=validateSthing()
if(success==False):
msg="sthing failed"
return (success,msg)

dosomeprocessing()
.....
success=validateSthingelse()
if(success==False):
msg="sthingelse failed"
return (success,msg)
domoreprocessing()
....
return(success,msg)

i would like to know if this way of doing this is OK..I have need of
many kinds of validations in this ..is there a better way of doing
this ?



我的关于我要说的第一件事的哲学编程帽子(作为一个相当开始的python程序员的b $ b)是避免从

函数/方法中多次返回,如果有的话可能" ;.他们会产生各种各样的问题

和错误,特别是如果有任何清理你需要

在很多地方做这件事(或者你忘记了)它在某些地方)。


所以: -


def mymethod(个体经营):

msg =" ; sthing失败"

success = validateSthing()

如果成功:

dosomeprocessing()

... ..

success = validateSthingelse()

如果成功:

domoreprocessing()

....

msg ="所有验证确定"

返回(成功,消息)


我丢失了不同的消息不同的错误,但你得到了

的想法。

如果成功:而不是if(success == True),更具可读性。对于

相反的如果不成功:。


-

Chris Green

With my philosophical programming hat on the first thing I''d say (as a
fairly beginning python programmer) is "avoid multiple returns from a
function/method if at all possible". They breed all sorts of problems
and errors, in particular if there''s any clearing up to do you have to
do it in lots of places (or you forget it in some places).

So:-

def mymethod(self):
msg="sthing failed"
success=validateSthing()
if success:
dosomeprocessing()
.....
success=validateSthingelse()
if success:
domoreprocessing()
....
msg="all validation OK"
return (success,msg)

I''ve lost the different messages for different errors but you get the
idea.
"if success:" rather than "if (success==True)", more readable. For
the opposite "if not success:".

--
Chris Green


2008年1月3日16:09:53 GMT,< ti ***** @ isbd.co.ukwrote:
On 03 Jan 2008 16:09:53 GMT, <ti*****@isbd.co.ukwrote:

>
ji*********@gmail.com 写道:
>
ji*********@gmail.com wrote:

我有一些代码,我设置了一个bool类型变量,如果值

为false我想从错误消息的方法..

作为一个初学者我想在这里得到一些帮助


class myclass:

... ......

def mymethod(self):

success = True

msg =" all validation OK"

success = validateSthing()

if(success == False):

msg =" sthing failed"

return (成功,消息)


dosomeprocessing()

.....

success = validateSthingelse()

if(success == False):

msg =" sthingelse failed"

return(success,msg)

domoreprocessing()

....

返回(成功,消息)


i想知道这种做法是否正常..我需要

这方面有多种验证..这有更好的方法吗?这需要吗?
i have some code where i set a bool type variable and if the value
is false i would like to return from the method with an error msg..
being a beginner I wd like some help here

class myclass:
.........
def mymethod(self):
success=True
msg="all validation OK"
success=validateSthing()
if(success==False):
msg="sthing failed"
return (success,msg)

dosomeprocessing()
.....
success=validateSthingelse()
if(success==False):
msg="sthingelse failed"
return (success,msg)
domoreprocessing()
....
return(success,msg)

i would like to know if this way of doing this is OK..I have need of
many kinds of validations in this ..is there a better way of doing
this ?



我的哲学编程帽子就是我要说的第一件事(作为一个相当开始的python程序员的b $ b)是避免如果可能的话,从

函数/方法中多次返回。他们会产生各种各样的问题

和错误,特别是如果有任何清理你需要

在很多地方做这件事(或者你忘记了)它在某些地方)。

With my philosophical programming hat on the first thing I''d say (as a
fairly beginning python programmer) is "avoid multiple returns from a
function/method if at all possible". They breed all sorts of problems
and errors, in particular if there''s any clearing up to do you have to
do it in lots of places (or you forget it in some places).



这个建议很有争议,并且在有例外的情况下

它充其量只是伏都教编码。既然你的函数可以在任何

点退出,无论你是否故意这样做,如果你有重要的

清理,最好以一种方式编写你的代码。是否正确

,即使你早点回来。遵循这种风格也经常导致奇怪的b / b $ b扭曲,比如额外的缩进层,以及

临时标志和价值持有者的扩散,如果你不需要以更直接的方式编写代码



根据具体情况做出决策的复杂性,

可读性和可靠性,而不是跟随

的声明(尤其是在不同的b / b
背景下发表的几十年的声明)。在下面的代码中强制使用单个返回站点会增加

复杂性,可论证会损害可读性,并且在

中提供*零*优势。

This advice is highly controversial, and in the presence of exceptions
it is, at best, voodoo coding. Since your function can exit at any
point whether you do it intentionally or not, if you have crucial
cleanup it''s best to write your code in a way that does it correctly
even if you return early. Following this style also often leads to odd
contortions, like extra layers of indentation, and a proliferation of
temporary flags and value-holders that aren''t necessary if you write
the code in a more straight forward manner.

Make your decisions on a case by case basis of complexity,
readability, and reliability instead of following pronouncements from
on high (especially decades old pronouncements made in a different
context). Forcing a single return site in the code below adds
complexity, arguable harms readability, and provides *zero* benefit in
the code at hand.


所以: -


def mymethod(self):

msg =" sthing failed"

success = validateSthing()

如果成功:

dosomeprocessing()

.....

success = validateSthingelse()

如果成功:

domoreprocessing()

....

msg =所有验证确定

返回(成功,消息)


我已经丢失了不同错误的不同消息,但是你得到了

idea。


" if if success:"而不是if(success == True),更具可读性。对于

相反的如果不成功:。


-

Chris Green


-
http:// mail.python.org/mailman/listinfo/python-list


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

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