filmov
tv
Python Namespace Explained in Code - Python Tutorial #10
![preview_player](https://i.ytimg.com/vi/kPCuT55GqrQ/maxresdefault.jpg)
Показать описание
After talking about the theory of namespaces and assignments in the last video I now want to look at namespaces in the actual code. With the dir() function we can take a look at all the names that currently exist in the namespace dir() is called in and when we assign a new name it will show up when calling dir(). The id() function on the other hand allows us to take a peek into the object space. id(name) gives us the address in memory of the object name is referencing.
The id() function is mostly for educational purposes since we don't have to care where exactly an object is living in memory but for mutable objects we care if two names refer to the same object. For that we use the 'is' keyword. This keyword is a boolean operator that returns True if both names refer to the same object. That is different from the equality operator which compares names by value rather than object location. Because the 'is' keyword compares object location : name1 is name2 is synonymous with
: id(name1) == id(name2) but it NOT synonymous with :name1 == name2
Python:
Outro Song:
Alright by Silent Partner is part of the YouTube Audio Library.
The id() function is mostly for educational purposes since we don't have to care where exactly an object is living in memory but for mutable objects we care if two names refer to the same object. For that we use the 'is' keyword. This keyword is a boolean operator that returns True if both names refer to the same object. That is different from the equality operator which compares names by value rather than object location. Because the 'is' keyword compares object location : name1 is name2 is synonymous with
: id(name1) == id(name2) but it NOT synonymous with :name1 == name2
Python:
Outro Song:
Alright by Silent Partner is part of the YouTube Audio Library.
Комментарии