1

I have class attributes in my class, that I want to set dynamically, here's a code example

class Something:
     attribute1 = 42                 # this is shared between all class instances
     def _init_(self, value):
          self.value = value

my question is: Is there a way to set that class attribute (attribute1) to some value, the same way that I can set my object instance attributes like this:

something = Something(value)
4
  • 3
    Something.attribute1 = somevalue? Commented May 1, 2019 at 15:19
  • Try use @classmethod Commented May 1, 2019 at 15:19
  • stackoverflow.com/questions/51992554/… Commented May 1, 2019 at 15:23
  • 2
    Note that _init_ should be __init__. Commented May 1, 2019 at 15:34

1 Answer 1

2

Yes just do

Something.attribute1 = "some value"    

Class attributes can be accessed via the class name. You can do this inside any function defined in the class or even outside of it.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.