关于使用isinstance的思考 [英] Thoughts on using isinstance

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

问题描述

在我的代码中,我正在讨论是否要验证传递给我的函数的数据类型

。例如


def sayHello(self,name):

如果不是名字:

rasie" name can''t如果不是isinstance(名称,str),则为null



raise名称必须是字符串

print" Hello" ; +名称


使用isinstance是坏还是做事的方式?这是一个重的b $ b操作吗?例如,如果我在每个函数中使用这个验证输入

它会减慢很多东西吗?


只是好奇你如何处理这种情况(除了

根本没有验证)。


谢谢

In my code I am debating whether or not to validate the types of data
being passed to my functions. For example

def sayHello(self, name):
if not name:
rasie "name can''t be null"
if not isinstance(name, str):
raise "name must be a string"
print "Hello " + name

Is the use of isinstance a "bad" way of doing things? is it a "heavy"
operation? for example, if I use this in each function validate input
will it slow things down a lot?

just curious how you might handle this type of situation (other than
not validating at all).

thanks

推荐答案




1月24日下午3:38,abcd < codecr ... @ gmail.comwrote:


On Jan 24, 3:38 pm, "abcd" <codecr...@gmail.comwrote:

在我的代码中,我正在讨论是否要验证数据类型

被传递给我的职能部门。例如


def sayHello(self,name):

如果不是名字:

rasie" name can''t如果不是isinstance(名称,str),则为null



raise名称必须是字符串

print" Hello" ; +名称


使用isinstance是坏还是做事的方式?这是一个重的b $ b操作吗?例如,如果我在每个函数中使用这个验证输入

它会减慢很多东西吗?


只是好奇你如何处理这种情况(除了

根本没有验证)。


谢谢
In my code I am debating whether or not to validate the types of data
being passed to my functions. For example

def sayHello(self, name):
if not name:
rasie "name can''t be null"
if not isinstance(name, str):
raise "name must be a string"
print "Hello " + name

Is the use of isinstance a "bad" way of doing things? is it a "heavy"
operation? for example, if I use this in each function validate input
will it slow things down a lot?

just curious how you might handle this type of situation (other than
not validating at all).

thanks



我的意见是验证通常很好。但是,你必须

使它不太严格。

例如,而不是


print" Hello" +名字


你可以写的


print" Hello" + str(name)


在这种情况下,要求isinstance()将过于严格。你必须检查的唯一

是hasattr(name,__ str __)和

可调用(name .__ str__)


在这种情况下,您可以进行验证,同时享受完全动态打字的灵活性。


-

Maxim

My opinion is that validation is generally good. However, you have to
make it not too strict.
For example, instead of

print "Hello " + name

you could have written

print "Hello " + str(name)

In this case requirement isinstance() will be too strict. The only
thing you have to check is that hasattr(name, "__str__") and
callable(name.__str__)

In this case you can have validation, while at the same time enjoy full
flexibility of dynamic typing.

--
Maxim


abcd写道:
abcd wrote:

在我的代码中,我正在辩论是否或不验证数据类型

传递给我的函数。例如


def sayHello(self,name):

如果不是名字:

rasie" name can''t如果不是isinstance(名称,str),则为null



raise名称必须是字符串

print" Hello" ; +名称


使用isinstance是坏还是做事的方式?这是一个重的b $ b操作吗?例如,如果我在每个函数中使用这个验证输入

它会减慢很多东西吗?


只是好奇你如何处理这种情况(除了

根本没有验证)。


谢谢
In my code I am debating whether or not to validate the types of data
being passed to my functions. For example

def sayHello(self, name):
if not name:
rasie "name can''t be null"
if not isinstance(name, str):
raise "name must be a string"
print "Hello " + name

Is the use of isinstance a "bad" way of doing things? is it a "heavy"
operation? for example, if I use this in each function validate input
will it slow things down a lot?

just curious how you might handle this type of situation (other than
not validating at all).

thanks



Python方式"是通过执行您需要的操作来验证

执行并捕获任何导致的异常。对于你的

例子,你似乎在说你宁愿提高你自己的

异常(顺便说一句,它应该是一个子类异常,

但是我们会忽略那些依赖于解释器来提出一个

ValueError或TypeError。这真的有什么优势吗?你

增加你的代码大小,并为执行时间添加*某些东西*

一点点真实目的。


人们来到Python在C ++或类似语言允许

或需要参数类型声明之后,通常不会感到舒服

从这个方向开始,但它适用于大多数我们。


问候

Steve

-

Steve Holden +44 150 684 7255 + 1 800 494 3119

Holden Web LLC / Ltd http://www.holdenweb .com

Skype:holdenweb http:/ /del.icio.us/steve.holden

博客注意: http://holdenweb.blogspot.com

The "Python way" is to validate by performing the operations you need to
perform and catching any exceptions that result. In the case of your
example, you seem to be saying that you''d rather raise your own
exception (which, by the way, should really be a subclass of Exception,
but we will overlook that) that relying on the interpreter to raise a
ValueError or a TypeError. Is there really any advantage to this? You
increase your code size and add *something* to execution time with
little real purpose.

People coming to Python after C++ or some similar language that allows
or requires parameter type declarations often don''t feel comfortable
taking this direction to start with, but it works well for most of us.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Blog of Note: http://holdenweb.blogspot.com


abcd写道:
abcd wrote:

在我的代码中,我正在讨论是否验证传递给我的函数的数据类型

。例如


def sayHello(self,name):

如果不是名字:

rasie" name can''t如果不是isinstance(名称,str),则为null



raise名称必须是字符串

print" Hello" ; +名称


使用isinstance是坏还是做事的方式?这是一个重的b $ b操作吗?例如,如果我在每个函数中使用这个验证输入

它会减慢很多东西吗?


只是好奇你如何处理这种情况(除了

根本没有验证)。


谢谢
In my code I am debating whether or not to validate the types of data
being passed to my functions. For example

def sayHello(self, name):
if not name:
rasie "name can''t be null"
if not isinstance(name, str):
raise "name must be a string"
print "Hello " + name

Is the use of isinstance a "bad" way of doing things? is it a "heavy"
operation? for example, if I use this in each function validate input
will it slow things down a lot?

just curious how you might handle this type of situation (other than
not validating at all).

thanks



Python方式"是通过执行您需要的操作来验证

执行并捕获任何导致的异常。对于你的

例子,你似乎在说你宁愿提高你自己的

异常(顺便说一句,它应该是一个子类异常,

但是我们会忽略那些依赖于解释器来提出一个

ValueError或TypeError。这真的有什么优势吗?你

增加你的代码大小,并为执行时间添加*某些东西*

一点点真实目的。


人们来到Python在C ++或类似语言允许

或需要参数类型声明之后,通常不会感到舒服

从这个方向开始,但它适用于大多数我们。


问候

Steve

-

Steve Holden +44 150 684 7255 + 1 800 494 3119

Holden Web LLC / Ltd http://www.holdenweb .com

Skype:holdenweb http:/ /del.icio.us/steve.holden

博客注意: http://holdenweb.blogspot.com

The "Python way" is to validate by performing the operations you need to
perform and catching any exceptions that result. In the case of your
example, you seem to be saying that you''d rather raise your own
exception (which, by the way, should really be a subclass of Exception,
but we will overlook that) that relying on the interpreter to raise a
ValueError or a TypeError. Is there really any advantage to this? You
increase your code size and add *something* to execution time with
little real purpose.

People coming to Python after C++ or some similar language that allows
or requires parameter type declarations often don''t feel comfortable
taking this direction to start with, but it works well for most of us.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Blog of Note: http://holdenweb.blogspot.com


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

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