Immutable vs Mutable Objects in Python
https://dbader.org/python-tricks ► Improve your Python skills, one bite at a time and write Pythonic and beautiful code.
In Python, immutable vs mutable data types and objects types can cause some confusion—and weird bugs. With this video you'll see what the difference between mutable and immutable data types is in Python, and how you can use it to your advantage in your own programs.
You'll also see how to deal with a language quirk in Python that allows objects referenced by immutable types to me modified. Python's definition of "immutable" can be a bit misleading:
Basically, the promise of "immutability" on tuples is only partly true. The tuple itself cannot be modified, but objects referenced by the tuple can be. This is sometimes called "non-transitive immutability."
If the tuple has an immutable field like a string for example, then it cannot be modified. A mutable field like a list however, can be edited, even if it's embedded in the "immutable" tuple.
When the Python documentation refers to an object as being "immutable" they mean the behavior above observed. Other immutable types in Python behave the same way, e.g. namedtuples or frozensets.
If you've ever encountered the following exception and now you want to know why, this is the video for you:
TypeError: 'tuple' object does not support item assignment
* * *
► Python MUGS, T-SHIRTS & MORE: https://nerdlettering.com
FREE Python Tutorials & News:
» Python Tutorials: https://dbader.org
» Python News on Twitter: https://twitter.com/@dbader_org
» Weekly Tips for Pythonistas: https://dbader.org/newsletter
» Subscribe to this channel: https://dbader.org/youtube
Видео Immutable vs Mutable Objects in Python канала Real Python
In Python, immutable vs mutable data types and objects types can cause some confusion—and weird bugs. With this video you'll see what the difference between mutable and immutable data types is in Python, and how you can use it to your advantage in your own programs.
You'll also see how to deal with a language quirk in Python that allows objects referenced by immutable types to me modified. Python's definition of "immutable" can be a bit misleading:
Basically, the promise of "immutability" on tuples is only partly true. The tuple itself cannot be modified, but objects referenced by the tuple can be. This is sometimes called "non-transitive immutability."
If the tuple has an immutable field like a string for example, then it cannot be modified. A mutable field like a list however, can be edited, even if it's embedded in the "immutable" tuple.
When the Python documentation refers to an object as being "immutable" they mean the behavior above observed. Other immutable types in Python behave the same way, e.g. namedtuples or frozensets.
If you've ever encountered the following exception and now you want to know why, this is the video for you:
TypeError: 'tuple' object does not support item assignment
* * *
► Python MUGS, T-SHIRTS & MORE: https://nerdlettering.com
FREE Python Tutorials & News:
» Python Tutorials: https://dbader.org
» Python News on Twitter: https://twitter.com/@dbader_org
» Weekly Tips for Pythonistas: https://dbader.org/newsletter
» Subscribe to this channel: https://dbader.org/youtube
Видео Immutable vs Mutable Objects in Python канала Real Python
Показать
Комментарии отсутствуют
Информация о видео
Другие видео канала
Python Immutable and Mutable TypesProgramming Terms: Mutable vs ImmutableMemory Allocation and Management in Python - simplified tutorial for beginnersOptional Arguments in Python With *args and **kwargsEmulating switch/case Statements in Python with DictionariesPython: Data Structures - Lists, Tuples, Sets & Dictionaries tutorial"is" vs "==" in Python – What's the Difference? (And When to Use Each)String Conversion in Python: When to Use __repr__ vs __str__9.9 String is Immutable10 Python Tips & Tricks That You Do Not Know || Python Clever Tips || Learn Python ShortcutsComposition over InheritanceJava Tutorials: Why String is Immutable in JavaMutable vs Immutable - PythonData Structures In Python | List, Dictionary, Tuple, Set In Python | Python Training | EdurekaPython Tutorial for Beginners 5: Dictionaries - Working with Key-Value PairsMastering Python - Everything You Need To Know To Become a Python MasterFunctional Programming in Python: Immutable Data StructuresPython Tutorial for Beginners 4: Lists, Tuples, and Sets