3

I am trying to add class attributes dynamically, but not at the instance level. E.g. what I can do manually as:

class Foo(object):
    a = 1
    b = 2
    c = 3

I'd like to be able to do with:

class Foo(object):
    dct = {'a' : 1, 'b' : 2, 'c' : 3}
    for key, val in dct.items():
        <update the Foo namespace here>

I'd like to be able to do this without a call to the class from outside the class (so it's portable), or without additional classes/decorators. Is this possible?

3 Answers 3

5

Judging from your example code, you want to do this at the same time you create the class. In this case, assuming you're using CPython, you can use locals().

class Foo(object):
    locals().update(a=1, b=2, c=3)

This works because while a class is being defined, locals() refers to the class namespace. It's implementation-specific behavior and may not work in later versions of Python or alternative implementations.

A less dirty-hacky version that uses a class factory is shown below. The basic idea is that your dictionary is converted to a class by way of the type() constructor, and this is then used as the base class for your new class. For convenience of defining attributes with a minimum of syntax, I have used the ** convention to accept the attributes.

def dicty(*bases, **attrs):
    if not bases:
        bases = (object,)
    return type("<from dict>", bases, attrs)

class Foo(dicty(a=1, b=2, c=3)):
    pass

# if you already have the dict, use unpacking

dct = dict(a=1, b=2, c=3)

class Foo(dicty(**dct)):
    pass

This is really just syntactic sugar for calling type() yourself. This works fine, for instance:

 class Foo(type("<none>", (object,), dict(a=1, b=2, c=3))):
     pass
Sign up to request clarification or add additional context in comments.

6 Comments

While this works on current CPython, it is not guaranteed to work on other Python implementations or future versions of CPython. You should never modify the dicitonary returned by locals(). (Modifying globals() is fine, though. At least it is guaranteed to work.)
+1: this is great! So much better than the messy metaclass solution I was proposing. I'd give +10 for the reasoning ("refers to the class namespace") behind it if I could! TIL tons of stuff. Thank you internet!
@DarenThomas: I wouldn't call something explicitly forbidden by the documentation a "great" solution. I would rather call it a dirty hack that happens to work by mere chance.
Well, the documentation says this dictionary "should not" be modified, which is hardly "forbidden." (I'm pretty sure it says this because much of the time, it doesn't do what you expect.) It doesn't work by "chance" either -- it's an implementation detail. Dirty hack? Yes, I'll own that.
Added a second method that's less dirty and hacky. :-)
|
2

Do you mean something like this:

def update(obj, dct):
    for key, val in dct.items():
        obj.setattr(key, val)

Then just go

update(Foo, {'a': 1, 'b': 2, 'c': 3})

This works, because a class is just an object too ;)

If you want to move everything into the class, then try this:

class Foo(object):
    __metaclass__ = lambda t, p, a: return type(t, p, a['dct'])
    dct = {'a': 1, 'b': 2, 'c': 3}

This will create a new class, with the members in dct, but all other attributes will not be present - so, you want to alter the last argument to type to include the stuff you want. I found out how to do this here: What is a metaclass in Python?

12 Comments

It's a matter of taste, but I'd prefer to use a class method instead of a function.
Both of these (using setattr through an update function or classmethod) are fine, but they require a call outside of the class - for portability i'd prefer to do it all within the class's startup, if it's possible
oh, ok, then I think I misunderstood you. Your solution above works just fine: The code will be run when the class definition is executed. This is the same mechanism that sets up class variables and methods.
but i can't figure out the mechanism to set the attributes. In other words, I can easily manually set a = 1, but with just a dictionary {'a' : 1} this is becomes more difficult, since the setattr function requires the object as its first argument (which i can't provide within the class definition). I feel like something with the setattr method, or using metaclasses is the way to go, but I can't figure it out.
if you run update in your module mymodule the from mymodule import Foo will work correctly
|
2

The accepted answer is a nice approach. However, one downside is you end up with an additional parent object in the MRO inheritance chain that isn't really necessary and might even be confusing:

>>> Foo.__mro__
(<class '__main__.Foo'>, <class '__main__.<from dict>'>, <class 'object'>)

Another approach would be to use a decorator. Like so:

def dicty(**attrs):
    def decorator(cls):
        vars(cls).update(**attrs)
        return cls
    return decorator

@dicty(**some_class_attr_namespace)
class Foo():
    pass

In this way, you avoid an additional object in the inheritance chain. The @decorator syntax is just a pretty way of saying:

Foo = dicty(a=1, b=2, c=3)(Foo)

1 Comment

Lovely approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.