Python Forum
dynamic use of attributes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dynamic use of attributes
#1
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'].
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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)
Reply
#3
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020