English 中文(简体)
严格比较
原标题:Strict comparison

在javascript,有严格的比较操作者op1 == op2op1 != op2,将两者的类型和价值进行比较。 难道有朝一方向迈进吗?

迄今为止,我只能提出以下重大条件:

isinstance(op1, type(op2)) 以及isinstance(op2, type(op1)) 以及op1 == op2

以及

not isinstance(op1, type(op2)) or not isinstance(op2, type(op1)) or op1 != op2
最佳回答

你的做法确实将检查价值和类型。 位于沙尔的有不同的经营者。

据说,在许多情况下,如果你不想要的话,那么就应该将任何作为uck子的物体视为uck。 你们往往不只想听说过的话,你们想要的是“类似”的物体,这样,只要目标能够用于特定任务,那么守则就应当接受。

问题回答

Python s equal comparator is strict except for when comparing 1 to True, and 0 to False, and it doesn t matter if the value for 1 or 0 is of type float, decimal.Decimal, or long. Zero of any numeric type, for example, 0, 0L, 0.0, 0j is always False. (Note that anything else cast to a bool is True. See Truth Value Testing in Python.) 1 of any type except complex (1L, 1.0, 1) is always True.

扎里

0 ==  0   # False
0 ==  0  and type(0) == type( 0 )  # False, compare short circuits 
0 ==     # False
0 ==    and type(0) == type(  )  # False, compare short circuits 

1 == True and type(1) == type(True)  # False, makes a difference here
1 == True  # True, also true if 1 was 1.00, etc..
0 == False  # True
False == None  # False
0 == bool(None)  # True

第一次比较返回法列斯时,没有对第二次比较进行评价,因此,比较短的路段是0,而其他东西都是0。 这样做是不必要的,但只有在将1比6行为真实时才适用。

Java:

0 ==  0   //true
0 ===  0   //false
0 ==     //true
0 ===  0  //false

1 === true //false
1 == true //true
0 == false //true
false == null //false
0 == !!(null) //true

因此,与贾瓦语文本的最接近之处是:

a == b and type(a) == type(b)

但是,只有在比照1或0时(不可能)的情况下才能使用。 如果你期望某个数值是算数,或是一种ool,那么你可能想确定你的代码。 mistake误就是发生这样的情况:

a = 0.0  # a valid value, lets assume it comes from a source that can also return None and we have no control over that.

# Should be:
# if a not None:
if a: # a is cast to bool, bool(0.0) is False
    print "do something here..."

仅澄清一些混淆,其了解Sharma s is<>/em>的操作者的好处。 粉碎机有is的操作器,可回收 如果s两边均受同一物体约束,否则就会归还法列斯。 在使用直言词时,物体的寿命仅为说明。 因此,在插字面上的is是安全的,因为它们相同,被分配到同一物体上。 这也适用于其他不易变的类型,如ool,以及所有类型:

0 is  0   # False
0 is False  # False
0 is 0  # True

在比较两个变数或变量和字面时,不能保证这样做。

当你制造两个空洞名单时,你会拿到两个不同的物体。

x = []
y = []
x is y  # False

但在这种情况下,这些变量参照了同一个清单,并将继续这样做,直到重新签字,或从另一个变量中提取一份:

x = y = []
x is y  # True
x.append(1)
x is y  # True
x = [1, ]
x is y  # False, even though the value is same

<>s 运营商正在比较物体的特性,并从事以下工作:

id( 0 ) == id(0)

因此,如果两个物体都提及同一记忆,它们就是指同一物体,因此必须相同。

为了避免is进行严格比较的良好想法,除非你想要检查这两个物体是否都提到同样的记忆。

正如Simon的回答所说,关于平等问题的沙捞法哲学不同于Java的圣经,实际上不需要一个严格的平等参照点。 平等参比器并不像Javas”(=)松散,但同时又不与=完全相同。

只要你清楚知道,任何数字类型(0、0L、0.0、0j)的零,总是等于法勒,除复杂数字(1、1L、1.0)外,任何数字类型中,只有1个是真的。

平等参比者大多是严格的。

例如:

Python

0 ==  0   # False
0 ==     # False

Java

0 ==  0   //True
0 ===  0   //False
0 ==     //True
0 ===  0  //False

EDIT:你不能创建新的运营商, p不支持。 然而,运营商实际上在发挥作用,而且你可以(我认为)凌驾于他们之上。

如果你对价格进行对比,那么你常常可以使用,尽管这并不严格与你的要求相同。 有一些 go,但会“一般工作”。

如果你不比较价格,而且你已经分两门课,那么你可以控制其compare equality

原定员额:

You can also use the operator module if you want to be super strict. https://docs.python.org/2/library/operator.html

>>> import operator
>>> operator.eq(True, 1)
True
>>> operator.is_(True, 1)
False

这里的一些答案是错误的。 例如,就某些比较而言,甲壳不会区分某些类型。

例如:

>>> 1 == 1.0
True
>>> operator.eq(1, 1.0)
True
>>> operator.is_(1, 1.0)
False

工作做得好于以下(或==),但取决于一个变数,即点数与值相同,这意味着你喜欢的许多案例。

如果你想要深入,以简便的方式执行,例如:。 你们将建设自己的经营者。

In python there is only one strict comparison operator that is == Suppose we have 2 cases:

>>> 2 ==  2 
False

# Comparison of Int with string, it returns False
>>> 2 == 2.0
True

# Comparison of Int with Float, it returns True

However in other programming languages like Julia, There is a distinction between comparison operator and strict comparison operator.

>>> 2 == 2.0
true

>>> 2 === 2.0
false

# strict comparison operator, namely === which is true only if two values agree fully as to type as well as value.




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签