What is the pythonic way to add type information to an object's attributes?
Posted
by Tikitu
on Stack Overflow
See other posts from Stack Overflow
or by Tikitu
Published on 2010-05-21T09:27:07Z
Indexed on
2010/05/21
9:30 UTC
Read the original article
Hit count: 206
I'm building classes where I know the types of the attributes, but Python of course doesn't. While it's un-pythonic to want to tell it, supposing I do want to, is there an idiomatic way to do so?
Why: I'm reading in serialised data (without type information) involving objects-nested-inside-objects. It's easy to put it into nested dictionaries, but I want it in objects of my class-types, to get the right behaviours as well as the data. For instance: suppose my class Book
has an attribute isbn
which I will fill with an ISBNumber
object. My serialised data gives me the isbn as a string; I would like to be able to look at Book
and say "That field should be filled by ISBNumber(theString)
."
Bonus glee for me if the solution can be applied to classes I get from someone else without editing their code.
© Stack Overflow or respective owner