adding properties dynamically (how to?)

  • Thread starter Bruno Desthuilliers
  • Start date
B

Bruno Desthuilliers

André a écrit :
I didn't want to hijack the original thread but I have basically the
same request...

akonsu a écrit :> hello,
[SNIP]

Wrong solution to your problem, I'd say. Let's start again:

"""
i need to add properties to instances dynamically during run time.
this is because their names are determined by the database contents.
"""

Care to elaborate ? I may be wrong, but I suspect you're trying to roll
your own python/database mapper. If so, there are quite a couple Python
ORMs around. Else, please tell us more.

I'm not the original poster, but I'd like to do the same thing (for a
different reason).

I have a program (crunchy) that is extensible via plugins. New
options available via plugins can be turned on or off (or selected
among a list of options). I have a module for user preferences (let's
call it prefs.py) that allows the setting of these options (and do
error checking, automatic saving of the options selected for future
sessions, etc.). These options are implemented as properties.

Currently I have it simplified so that only two lines need to be added
to prefs.py to add new options; something like
options = { ...
'new_option': [value1, value2, ..., valueN],
...}

and
class Preferences(object):
...

new_option = make_property('new_option', 'some nicely worded help
string')

===
make_property is a custom define function that return fgets, fsets,
fdel and doc.

You may want to write your own descriptor object instead. The 'property'
class is just one possible way to use the descriptor protocol for
"smart" attributes.
Ideally, I'd like to be able to define new would-be properties from
the plugin and add them to the class prior to creating instances. In
other words, have something like

===
for option in options_defined_in_plugins:
add_option_as_property_to_Preferences(Preferences, option, ...)

user_preferences = Preferences()

Not the same problem as the OP here. Your properties are not
per-instance, so just adding them to the class is ok. Remember that in
Python, classes are objects too, and (a couple corner cases set aside)
nothing prevent you from dynamically adding attributes to an object. The
following snippets are equivalent:

class Foo1(object):
attrib = object()

class Foo2(object):
pass

Foo2.attrib = object()

class Foo3(object):
pass

setattr(Foo3, 'attrib', object())
 
A

André

I didn't want to hijack the original thread but I have basically the
same request...

akonsu a écrit :> hello,
[SNIP]


Wrong solution to your problem, I'd say. Let's start again:

"""
i need to add properties to instances dynamically during run time.
this is because their names are determined by the database contents.
"""

Care to elaborate ? I may be wrong, but I suspect you're trying to roll
your own python/database mapper. If so, there are quite a couple Python
ORMs around. Else, please tell us more.

I'm not the original poster, but I'd like to do the same thing (for a
different reason).

I have a program (crunchy) that is extensible via plugins. New
options available via plugins can be turned on or off (or selected
among a list of options). I have a module for user preferences (let's
call it prefs.py) that allows the setting of these options (and do
error checking, automatic saving of the options selected for future
sessions, etc.). These options are implemented as properties.

Currently I have it simplified so that only two lines need to be added
to prefs.py to add new options; something like
options = { ...
'new_option': [value1, value2, ..., valueN],
...}

and
class Preferences(object):
...

new_option = make_property('new_option', 'some nicely worded help
string')

===
make_property is a custom define function that return fgets, fsets,
fdel and doc.

Ideally, I'd like to be able to define new would-be properties from
the plugin and add them to the class prior to creating instances. In
other words, have something like

===
for option in options_defined_in_plugins:
add_option_as_property_to_Preferences(Preferences, option, ...)

user_preferences = Preferences()

Performance in this case would not be an issue.

Cheers,

André
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top