Python Forum
dynamic use of attributes - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: dynamic use of attributes (/thread-13498.html)



dynamic use of attributes - Skaperen - Oct-18-2018

a while back i saw a web page that showed how to create an object in which attributes can be created by an assignment to that attribute.

you would get the nice object from a call to a function or a class such as foo = objectmaker(). thwn you could assign directly to attributes of that object like foo.bar = 5 and so on. i can't find that page, now. does anyone know how this is done and can explain how?

i got it to work using ordinary dictionary syntax. i'm still guessing-and-trying to get it to work with attribute syntax.

en.py:
def en():
    import common
    c = common.share()
    c['foo'] = 'faen'
    return
to.py:
def to():
    import common
    c = common.share()
    print(c['foo'])
    return
hoved.py:
from en import en
from to import to
en()
to()
common.py:
def share():
    return globals()
the above works but i want to use c.foo instead of c['foo'].


RE: dynamic use of attributes - Gribouillis - Oct-18-2018

You could simply use the common module
def en():
    import common
    common.foo = 'faen'
You can also play with
>>> class Share:
...     def __init__(self):
...         self.__dict__ = globals()
... 
>>> share = Share()
>>> share.foo = 'bar'
>>> foo
'bar'
Remember that "Simple is better than complex" (the Zen of Python)


RE: dynamic use of attributes - Skaperen - Oct-18-2018

in simple vs. complex, sometimes doing complex now makes the future simple. if you have some complex (as in complicated) stuff to do, encapsulating it in a function make life simple: now just call the function. make the function even more complex but working correctly with more situations can make life even simpler: in more cases just call the function.

sometimes simplicity is in the eye of the beholder.

and common.py doesn't even need anything in it; an empty file works.


RE: dynamic use of attributes - ichabod801 - Oct-19-2018

If you just want something you can assign any attribute to, subclass object:

>>> class Foo(object):
...     pass
...
>>> foo = Foo()
>>> foo.bar = 5
>>> foo.bar
5
Oddly enough, this doesn't work with object itself:

>>> obj = object()
>>> obj.bar = 5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute 'bar'
Another handy one creates and assigns any attributes you pass it:

class Morphling(object):

    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)
>>> morph = Morphling(foo = 5)
>>> morph.foo
5
I use that type of class to make dummy objects for unit testing.


RE: dynamic use of attributes - Skaperen - Oct-19-2018

i want more than just being able to assign any attribute. i want to also have separate modules being able to share it. and other modules that do this with a different name (probably chosen by the developer) have their between-module sharing kept separate to avoid name collisions.