Custom classes/objects¶
Copying¶
Deep copying¶
Shallow copying¶
namedtuple¶
namedtuple API¶
- always starts with _prefix to avoid conflicts with arbitrarily user defined fields
- should still use them!
Converted namedtuple to dict¶
my_car._asdict()
OrderedDict([('color', 'red'), ('mileage', 3812.4)])
json.dumps(my_car._asdict())
'{"color": "red", "mileage": 3812.4}'
Extending a namedtuple with _fields¶
Car = namedtuple('Car', 'color mileage')
ElectricCar = namedtuple('ElectricCar', Car._fields + ('charge',))
"Edit" a namedtuple¶
- shallow copies and replaces some fields
Created a namedtuple from an iterable¶
When to use namedtuples¶
- only if it makes the code cleaner
- alternatives- dataclass (if you don't want immutability)
- dict or list
 
NamedTuple¶
Python 3.6+
from typing import NamedTuple
class Car(NamedTuple):
    color: str
    mileage: float
    automatic: bool
car1 = Car('red', 3812.4, True)
>>> car1.mileage
3812.4
# Fields are immutable:
>>> car1.mileage = 12
AttributeError: "can't set attribute"
>>> car1.windshield = 'broken'
AttributeError:
"'Car' object has no attribute 'windshield'"
# Type annotations are not enforced without
# a separate type checking tool like mypy:
>>> Car('red', 'NOT_A_FLOAT', 99)
Car(color='red', mileage='NOT_A_FLOAT', automatic=99)
read-only attribute¶
- use a @property
class Square:
    def __init__(self, length):
        self._length = length
    @property
    def length(self):
        return self._length
  
    
      Last update:
      2023-04-24