Dariusz Krynicki
Dariusz Krynicki

Reputation: 2718

python dataclass no attribute post_init

When I run the code below:

import pandas as pd
from dataclasses import dataclass


@dataclass
class SomeClass:
    df_in: pd.DataFrame

    def __post__init__(self):
        self.b = 1


if __name__ == '__main__':
    df_in = pd.DataFrame([])
    p = SomeClass(df_in=df_in)
    p.b

I get

AttributeError: 'SomeClass' object has no attribute 'b'

Why?

EDIT:

Am I using post__init correctly as there is no init in dataclasses

Upvotes: 0

Views: 1952

Answers (1)

ilmarinen
ilmarinen

Reputation: 5707

Your __post_init__ function is misnamed. You should only have one underscore between post and init, but you have two.

Upvotes: 5

Related Questions