Skip to main content
Dataclasses (Python 3.7)
Source Link
fralau
  • 3.6k
  • 3
  • 29
  • 44

Update (September 2020)

@PPC: your dream has come true.

Since Python 3.7, a new tool is available as a standard: dataclasses (unsurprisingly, the designer of the named list package, Eric V. Smith, is also behind it).

In essence, it provides an automatic initialization of class variables.

from dataclasses import dataclass

@dataclass
class InventoryItem:
    """Class for keeping track of an item in inventory."""
    name: str
    unit_price: float
    quantity_on_hand: int = 0

    def total_cost(self) -> float:
        return self.unit_price * self.quantity_on_hand

(from the official doc)

What the @dataclass decorator will do, will be to automatically add the __init__() method:

def __init__(self, name: str, unit_price: float, quantity_on_hand: int=0):
    self.name = name
    self.unit_price = unit_price
    self.quantity_on_hand = quantity_on_hand

IMHO, it's a pretty, eminently pythonic solution.

Eric also maintains a backport of dataclasses on github, for Python 3.6.


Update (September 2020)

@PPC: your dream has come true.

Since Python 3.7, a new tool is available as a standard: dataclasses (unsurprisingly, the designer of the named list package, Eric V. Smith, is also behind it).

In essence, it provides an automatic initialization of class variables.

from dataclasses import dataclass

@dataclass
class InventoryItem:
    """Class for keeping track of an item in inventory."""
    name: str
    unit_price: float
    quantity_on_hand: int = 0

    def total_cost(self) -> float:
        return self.unit_price * self.quantity_on_hand

(from the official doc)

What the @dataclass decorator will do, will be to automatically add the __init__() method:

def __init__(self, name: str, unit_price: float, quantity_on_hand: int=0):
    self.name = name
    self.unit_price = unit_price
    self.quantity_on_hand = quantity_on_hand

IMHO, it's a pretty, eminently pythonic solution.

Eric also maintains a backport of dataclasses on github, for Python 3.6.

added 41 characters in body
Source Link
fralau
  • 3.6k
  • 3
  • 29
  • 44

Let's say its the "quick and dirty way", which couldwould work in a number of cases. In general there is not even the need to declare your class.

But what if you want that instance to be mutable, i.e. to allow you update the elementsproperties of the instance? The answer is the named list (also known as RecordClass). Conceptually it is like a normal list, where the items are also accessible with the dot notation.

If you use them well, this might significantsignificantly cut down time spent on writing classes and handling instances!

Let's say its the "quick and dirty way", which could work in a number of cases. In general there is not even the need to declare your class.

But what if you want that instance to be mutable, i.e. update the elements of the instance? The answer is the named list (also known as RecordClass). Conceptually a list where the items are accessible with the dot notation.

If you use them well, this might significant cut down time spent on writing classes and handling instances!

Let's say its the "quick and dirty way", which would work in a number of cases. In general there is not even the need to declare your class.

But what if you want that instance to be mutable, i.e. to allow you update the properties of the instance? The answer is the named list (also known as RecordClass). Conceptually it is like a normal list, where the items are also accessible with the dot notation.

If you use them well, this might significantly cut down time spent on writing classes and handling instances!

added 364 characters in body
Source Link
fralau
  • 3.6k
  • 3
  • 29
  • 44
instance = SimpleNamespace()
instance.var1 = var1
instance.var2 = var2
instance = AClass(var1, var2)

or

instance = AClass(var1=var1, var2=var2)
instance = AClass(var1, var2)

or:

instance = AClass(var1=var1, var2=var2)

And you can then modify them:

instance.var1 = var3

But you can't add an attribute that is not defined.

>>> instance.var4 = var4
  File "<stdin>", line 1, in <module>
AttributeError: 'instance' object has no attribute 'var4'
instance.var1 = var1
instance.var2 = var2
instance = AClass(var1, var2)
instance = AClass(var1, var2)
instance = SimpleNamespace()
instance.var1 = var1
instance.var2 = var2
instance = AClass(var1, var2)

or

instance = AClass(var1=var1, var2=var2)
instance = AClass(var1, var2)

or:

instance = AClass(var1=var1, var2=var2)

And you can then modify them:

instance.var1 = var3

But you can't add an attribute that is not defined.

>>> instance.var4 = var4
  File "<stdin>", line 1, in <module>
AttributeError: 'instance' object has no attribute 'var4'
added 53 characters in body
Source Link
fralau
  • 3.6k
  • 3
  • 29
  • 44
Loading
deleted 8 characters in body
Source Link
fralau
  • 3.6k
  • 3
  • 29
  • 44
Loading
added 257 characters in body
Source Link
fralau
  • 3.6k
  • 3
  • 29
  • 44
Loading
edited body
Source Link
fralau
  • 3.6k
  • 3
  • 29
  • 44
Loading
added 83 characters in body
Source Link
fralau
  • 3.6k
  • 3
  • 29
  • 44
Loading
added 119 characters in body
Source Link
fralau
  • 3.6k
  • 3
  • 29
  • 44
Loading
added 60 characters in body
Source Link
fralau
  • 3.6k
  • 3
  • 29
  • 44
Loading
added 1334 characters in body
Source Link
fralau
  • 3.6k
  • 3
  • 29
  • 44
Loading
added 1334 characters in body
Source Link
fralau
  • 3.6k
  • 3
  • 29
  • 44
Loading
Source Link
fralau
  • 3.6k
  • 3
  • 29
  • 44
Loading