Главная страница » Что является результатом is not в python

Что является результатом is not в python

  • автор:

Операторы is-not и is в Python

Python не проверяет, относятся ли два операнда к одной и той же ссылке. Если у них нет одинаковой ссылки на память, python не возвращает True, иначе он возвращает False.

Пример

В следующем примере мы будем использовать оператор is not в операторе If.

В приведенном выше примере содержимое a и b одинаково, но a и b – разные объекты, имеющие разное распределение памяти.

Но когда a присваивается c, в c копируется только ссылка, но не все содержимое. Итак, если вы измените содержимое c, то содержимое a также будет изменено, потому что они относятся к одному и тому же списку в памяти. Следовательно, a не c – возвращается False, а a не b – возвращается True.

Оператор IS

Ключевое слово is в Python используется для проверки того, совпадают ли ссылки памяти двух объектов .

Оператор is принимает два операнда и возвращает True, если оба объекта имеют одинаковую ссылку на память, и False, если нет.

В следующем примере мы продемонстрируем использование и функционирование оператор is.

В приведенном выше примере содержимое a и b одинаково, но a и b – разные объекты, имеющие разное распределение памяти.

Но когда a присваивается c, в c копируется только ссылка, но не все содержимое. Итак, если вы измените содержимое c, то содержимое a также будет изменено, потому что они относятся к одному и тому же списку в памяти. Следовательно, a is c – возвращается True, а a b – возвращается False.

Заключение

В этом руководстве Python мы узнали, как использовать ключевое слово is-not и is в Python с помощью примеров программ.

Что является результатом is not в python

In this article, we are going to see != (Not equal) operators. In Python != is defined as not equal to operator. It returns True if operands on either side are not equal to each other, and returns False if they are equal. Whereas is not operator checks whether id() of two objects is same or not. If same, it returns False and if not same, it returns True. And is not operator returns True if operands on either side are not equal to each other, and returns false if they are equal.

Python '!=' Is Not 'is not': Comparing Objects in Python

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Comparing Python Objects the Right Way: "is" vs "=="

There’s a subtle difference between the Python identity operator ( is ) and the equality operator ( == ). Your code can run fine when you use the Python is operator to compare numbers, until it suddenly doesn’t. You might have heard somewhere that the Python is operator is faster than the == operator, or you may feel that it looks more Pythonic. However, it’s crucial to keep in mind that these operators don’t behave quite the same.

The == operator compares the value or equality of two objects, whereas the Python is operator checks whether two variables point to the same object in memory. In the vast majority of cases, this means you should use the equality operators == and != , except when you’re comparing to None .

In this tutorial, you’ll learn:

  • What the difference is between object equality and identity
  • When to use equality and identity operators to compare objects
  • What these Python operators do under the hood
  • Why using is and is not to compare values leads to unexpected behavior
  • How to write a custom __eq__() class method to define equality operator behavior

Python Pit Stop: This tutorial is a quick and practical way to find the info you need, so you’ll be back to your project in no time!

Free Bonus: Click here to get a Python Cheat Sheet and learn the basics of Python 3, like working with data types, dictionaries, lists, and Python functions.

Comparing Identity With the Python is and is not Operators

The Python is and is not operators compare the identity of two objects. In CPython, this is their memory address. Everything in Python is an object, and each object is stored at a specific memory location. The Python is and is not operators check whether two variables refer to the same object in memory.

Note: Keep in mind that objects with the same value are usually stored at separate memory addresses.

You can use id() to check the identity of an object:

The last line shows the memory address where the built-in function id itself is stored.

There are some common cases where objects with the same value will have the same id by default. For example, the numbers -5 to 256 are interned in CPython. Each number is stored at a singular and fixed place in memory, which saves memory for commonly-used integers.

You can use sys.intern() to intern strings for performance. This function allows you to compare their memory addresses rather than comparing the strings character-by-character:

The variables a and b initially point to two different objects in memory, as shown by their different IDs. When you intern them, you ensure that a and b point to the same object in memory. Any new string with the value ‘hello world’ will now be created at a new memory location, but when you intern this new string, you make sure that it points to the same memory address as the first ‘hello world’ that you interned.

Note: Even though the memory address of an object is unique at any given time, it varies between runs of the same code, and depends on the version of CPython and the machine on which it runs.

Other objects that are interned by default are None , True , False , and simple strings. Keep in mind that most of the time, different objects with the same value will be stored at separate memory addresses. This means you should not use the Python is operator to compare values.

When Only Some Integers Are Interned

Behind the scenes, Python interns objects with commonly-used values (for example, the integers -5 to 256) to save memory. The following bit of code shows you how only some integers have a fixed memory address:

Initially, a and b point to the same interned object in memory, but when their values are outside the range of common integers (ranging from -5 to 256), they’re stored at separate memory addresses.

When Multiple Variables Point to the Same Object

When you use the assignment operator ( = ) to make one variable equal to the other, you make these variables point to the same object in memory. This may lead to unexpected behavior for mutable objects:

What just happened? You add a new element to a , but now b contains this element too! Well, in the line where b = a , you set b to point to the same memory address as a , so that both variables now refer to the same object.

If you define these lists independently of each other, then they’re stored at different memory addresses and behave independently:

Because a and b now refer to different objects in memory, changing one doesn’t affect the other.

Comparing Equality With the Python == and != Operators

Recall that objects with the same value are often stored at separate memory addresses. Use the equality operators == and != if you want to check whether or not two objects have the same value, regardless of where they’re stored in memory. In the vast majority of cases, this is what you want to do.

When Object Copy Is Equal but Not Identical

In the example below, you set b to be a copy of a (which is a mutable object, such as a list or a dictionary). Both variables will have the same value, but each will be stored at a different memory address:

a and b are now stored at different memory addresses, so a is b will no longer return True . However, a == b returns True because both objects have the same value.

How Comparing by Equality Works

The magic of the equality operator == happens in the __eq__() class method of the object to the left of the == sign.

Note: This is the case unless the object on the right is a subclass of the object on the left. For more information, check the official documentation.

This is a magic class method that’s called whenever an instance of this class is compared against another object. If this method is not implemented, then == compares the memory addresses of the two objects by default.

As an exercise, make a SillyString class that inherits from str and implement __eq__() to compare whether the length of this string is the same as the length of the other object:

Now, a SillyString ‘hello world’ should be equal to the string ‘world hello’ , and even to any other object with the same length:

This is, of course, silly behavior for an object that otherwise behaves as a string, but it does illustrate what happens when you compare two objects using == . The != operator gives the inverse response of this unless a specific __ne__() class method is implemented.

The example above also clearly shows you why it is good practice to use the Python is operator for comparing with None , instead of the == operator. Not only is it faster since it compares memory addresses, but it’s also safer because it doesn’t depend on the logic of any __eq__() class methods.

Comparing the Python Comparison Operators

As a rule of thumb, you should always use the equality operators == and != , except when you’re comparing to None :

Use the Python == and != operators to compare object equality. Here, you’re generally comparing the value of two objects. This is what you need if you want to compare whether or not two objects have the same contents, and you don’t care about where they’re stored in memory.

Use the Python is and is not operators when you want to compare object identity. Here, you’re comparing whether or not two variables point to the same object in memory. The main use case for these operators is when you’re comparing to None . It’s faster and safer to compare to None by memory address than it is by using class methods.

Variables with the same value are often stored at separate memory addresses. This means that you should use == and != to compare their values and use the Python is and is not operators only when you want to check whether two variables point to the same memory address.

Conclusion

In this tutorial, you’ve learned that == and != compare the value of two objects, whereas the Python is and is not operators compare whether two variables refer to the same object in memory. If you keep this distinction in mind, then you should be able to prevent unexpected behavior in your code.

If you want to read more about the wonderful world of object interning and the Python is operator, then check out Why you should almost never use “is” in Python. You could also have a look at how you can use sys.intern() to optimize memory usage and comparison times for strings, although the chances are that Python already automatically handles this for you behind-the-scenes.

Now that you’ve learned what the equality and identity operators do under the hood, you can try writing your own __eq__() class methods, which define how instances of this class are compared when using the == operator. Go and apply your newfound knowledge of these Python comparison operators!

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Comparing Python Objects the Right Way: "is" vs "=="

Get a short & sweet Python Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.

Python Tricks Dictionary Merge

About Joska de Langen

Joska is an Ordina Pythoneer who writes for Real Python.

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

Python IS NOT Operator

Python is not operation is simply the opposite of Python is.

Python is not checks if the two operands refer to the same memory reference. If they do not have the same memory reference, python is not returns True, else it returns False.

Example 1: Python IS NOT Operator

In the following example, we will use is not operator in the Python If statement.

Python Program

Output

In the above example, the contents of a and b are same, but a and b are different objects having a different memory allocation.

But when a is assigned to c, only the reference is copied to c, but not the entire contents. So, if you change the contents of c, then the contents of a also would be changed, because they refer to the same list in memory. Hence a is not c returned False while a is not b returned True.

Summary

In this tutorial of Python Examples, we learned how to use is-not in Python, with the help of example programs.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *