What do you think?
Rate this book
794 pages, Kindle Edition
First published January 25, 2015
Special methods in Python, often referred to as "magic methods" or "dunder methods" (short for "double underscore"), are methods that have double underscores at the beginning and end of their names, like __init__, __str__, and __add__. These methods are used to enable certain behaviors and operations on objects that are instances of a class. They allow you to define how your objects should respond to built-in functions and operators.
A hash is a fixed-size integer that uniquely identifies a particular value or object. Hashes are used in many areas of computer science and programming, such as in data structures like hash tables (which are used to implement dictionaries and sets in Python). The purpose of hashing is to quickly compare and retrieve data in collections that require fast lookups.
- Python provides a built-in hash() function that returns the hash value of an object.
- The hash() function works on immutable data types like integers, floats, strings, and tuples.
- For mutable types like lists or dictionaries, hash() is not applicable because their contents can change, making them unsuitable for use as keys in hash-based collections.
- The hash value of an immutable object (like a string or a tuple) remains constant throughout the program's execution, provided that the object itself doesn't change.
- Mutable objects cannot be hashed because their contents can change, leading to inconsistencies in hash values.
- Hashes are crucial for the performance of dictionaries and sets in Python. When you insert an item into a dictionary or set, Python uses the hash value to determine where to store the data. This allows for very fast lookups.
Dictionaries themselves are not hashable. This is because dictionaries in Python are mutable objects, meaning their contents can change after they are created
Keys must be hashable: This is a strict requirement because dictionaries use a hash table internally to store key-value pairs. The hash value of the key determines where the pair is stored.
Keys must be immutable: This immutability ensures that the hash value of a key remains consistent throughout its lifetime. If the key could change, it would disrupt the dictionary's internal hash table, leading to unpredictable behavior when trying to retrieve or store values.
The key in a dictionary cannot change directly because keys in a dictionary must be immutable. This means that once you assign a key to a dictionary, it must remain unchanged.
However, if you want to "change" a key, you would need to remove the old key-value pair and add a new key with the same value (or a modified value).
Interfaces, Protocols and ABCs
Duck typing is a dynamic typing concept based on the idea of "if it looks like a duck and quacks like a duck, it must be a duck." This means that the type or class of an object is determined by its behavior (i.e., the methods it implements) rather than its explicit type. In other words, if an object implements the necessary methods or properties required by the context, it's considered valid, regardless of its actual class or type.
No explicit type checks: Python doesn't require you to declare the type of an object, and duck typing allows you to use any object that has the required methods.
Runtime flexibility: Duck typing checks happen at runtime, so the system checks the object's methods or attributes during execution.
Error prone at runtime: Since there are no compile-time checks, errors related to missing methods are only discovered when the program is executed.
class Duck:
def quack(self):
print("Quack!")
class Dog:
def quack(self):
print("Bark that sounds like a quack!")
def make_it_quack(animal):
animal.quack() # Works as long as 'animal' has a 'quack' method
duck = Duck()
dog = Dog()
make_it_quack(duck) # Output: Quack!
make_it_quack(dog) # Output: Bark that sounds like a quack!
Goose typing (sometimes referred to as gradual typing) is an extension of duck typing that adds more structure by explicitly checking whether objects conform to a given interface, but without strictly requiring them to belong to a specific class or type. The term "goose typing" isn't as commonly used as duck typing or static typing but can be thought of as a middle ground between the two.
In Python, goose typing can be implemented through tools like protocols and abstract base classes (ABCs). These mechanisms allow objects to be treated as valid types if they conform to a specific interface, regardless of their concrete type.