Dynamically creating properties?

DevPlayer devplayer at gmail.com
Sun Oct 30 10:11:25 EDT 2011


To be honest, I was hoping someone would have posted a link to a well
known and tested recipe. You'd think this function would be in the
standard library or a specific Exception tied directly with setattr()
and getattr() (and possibly __getattr__(), __getattribute__(),
__setattr__())

The main thing I wanted to point out though is when you start using
dynamically named references, there's more to it then just letting a
dynamic file define it.

If there's a way to reference a set of data, it really shouldn't be
with a "dynamically named reference" too often.

Databases are a good example. Perhaps this is a better way for
example: If you have a bunch of tables in your DB -is- it better to
get the table def and create a Python class with dynamically named
"fields"?

Or is it better to create a Table class with name attribute and a
Field class with a name attribute (named "name")

SO instead of :
    field_name = xml_parse.get_next_field_name(xml_table_definition)
    my_table = Table()
    setattr(my_table, field_name,
empty_list_to_later_contain_field_data)

Perhaps:
    field_name = xml_parse.get_next_field_name(xml_table_definition)
    my_table = Table()
    my_table.fields[field_name] =
empty_list_to_later_contain_field_data
    # or
    my_table.add_field( Field(field_name) )




More information about the Python-list mailing list