Correct type for a simple "bag of attributes" namespace object
Akira Li
4kir4.1i at gmail.com
Sun Aug 3 10:22:09 EDT 2014
Albert-Jan Roskam <fomcl at yahoo.com.dmarc.invalid> writes:
> I find the following obscure (to me at least) use of type() useful
> exactly for this "bag of attributes" use case:
>>>> employee = type("Employee", (object,), {})
>>>> employee.name = "John Doe"
>>>> employee.position = "Python programmer"
You could write it as:
class Employee:
name = "Johh Doe"
position = "Python programmer"
It also makes it clear that `type()` returns a *class Employee*, not its
instance (Employee()) and therefore name, position are class attributes.
--
Akira
More information about the Python-list
mailing list