Let's say I have a python Dataclass:
@dataclass
class Person:
name: str
age: int
married: bool
And I add data to the fields say from an imaginary dictionary:
for row in something:
ob = Person(
name=row.name,
age=row.age,
married=row.married,
)
How do I, for example, make name
equal to None
or an empty string if name
is not given in the imaginary dictionary?
I tried something like:
name = row.name if row.name else None
but I got an Attribute error like: AttributeError: object has no attribute 'name'
Edit: I want to achieve this without necessarily setting the fields in the Person
dataclass to None