-1

I have a particular problem with objective python, I would like to have a configuration class (Config), so it could be used as a base class for other classes which will need configuration data. What I want do is to use this Config class to share once iniciated data among all inheriting classes. What is important for me, when class Inherit configuration data I want it to be able to use it as it owns, for ex:

class Config:
    a= None
    b= None

class A(Config):
    def __init__(self):
        a = 10

    def print_a(self):
        print(self.a)

And here is the first questione, how should I set values of a and b in A class? Another one is how to call them inside the class?

When I'm doing sth like this :

obj = A()
obj.a = 5

or when i add methon do A class, which sets a variable:

    class A(Config):
    def __init__(self):
        a = 10

    def print_a(self):
        print(self.a)

    def setA(self, val):
        a = val

and call :

obj = A()
obj.setA(12)

it does not change either A.a or Config.a

To sum up, my goal is to create a class with static variables (Config), and through inheritance I would like to obtain acces to those variables from another class A(Config), and use those variables as they were native class variables. It is also important that every change in obj = A() -> obj.a should change Config.a variable (the same when I change variable a insade class A).

Its sucha a confusing idea what I want to do, hope you understand. Also I am pretty new to python so there is a lot of I dont understand yet, try to be forgiving please :).

dante
  • 43
  • 5
  • 1
    While you can read a class attribute through all of its instances, you can only write instance attributes via an instance. – Klaus D. Mar 19 '20 at 10:05
  • 1
    `a = 5` assigns a local variable. `self.a = 5` assigns an *instance attribute*. `Config.a = 5` assigns a class attribute. `type(self).a = 5` assigns the attribute to the *current* class, which may be interesting when you do inheritance. – deceze Mar 19 '20 at 10:25
  • @deceze Thank you and sorry for mentioning you below stupid of me but I have not noticed your respond above. The problem was my misunderstanding of scopes, now I get it :) – dante Mar 19 '20 at 14:09

1 Answers1

0

IGNORE THIS, I MISUNDERSTOOD THE QUESTION, LEAVING OPEN FOR COMMENTS

Well as far as I can see, the problem is the lack of the use of self. In the methods A.setA() and A.__init__, you have to used self.a and thus have simply created a local variable for the method.

So a functioning version of the class A would look like this:

class A(Config):
    def __init__(self):
        self.a = 10    #As I mentioned, you need this `self.a` rather than just `a`

    def print_a(self):
        print(self.a)    #You got that right

    def setA(self, val):
        self.a = val    #Same here with `self.a`

Think of making a method as simply defining a variable of the function class, and that the method behaves the same as it would outside of a class apart from the additional self argument, which is used to access the attributes of the class.

Community
  • 1
  • 1
  • But is't it just creating a local variable a in class A? I would like to access the "static" variable a from Config class, so thats why i use just a= 10 - but it is just my intuition. Becouse we can do obj.a = 10. And I would like it to behave in a way that if variable a in A class is changed it is also changed in Config class and shared to other classes that inherit from Config class. – dante Mar 19 '20 at 10:15
  • When you make a class based off another, the base class merges with the class inheriting it, and the inheriting class, in this case `A`, gets all of the attributes from the base class, in this case `Config`. So even if you do not set the variable `a` in `A.__init__`, other methods (in the class `A`) can still access the variable `a` from `Config` as `self.a` – a_random_programmer Mar 19 '20 at 10:23
  • The thing is that when I am assigning variable a in class A __init__ method like you are suggesting : self.a = 10, variable a in Config doesnt eqal 10 it stays None, so it is not shared by all classes that inherits after Config. Now I am reading about cls word to use instead of self, maybe it will do? – dante Mar 19 '20 at 10:37
  • Oh, I have probably miunderstood your comment, but still even when I am assigning variable a from other than __init__ method it does not change value of variable a in Config. – dante Mar 19 '20 at 10:51
  • 1
    It seems I have misunderstood your question, and if I have now understood correctly, you must use @ deceze♦ 's advice on this and just set `Config.a`. – a_random_programmer Mar 19 '20 at 10:54
  • @deceze posts you have pinted does not solve my problem (as far as I understand them..) It helps in a way, couse they show how to acces class attribute if we declare this attribute in this class, but it still does not affect attribute in my Base class. Even when i do sth like this in A(Config) class : def _ _ init _ _ (self): A.a=10 it does not change variable a globally I mean Config.a is not changed and remain None, and this is what i am asking about. – dante Mar 19 '20 at 13:43
  • 1
    @dante I'm not going to see replies to me unless you comment in the same thread as me. And yes, if you set `A.a` that won't affect `Config.a`. So set `Config.a` instead!? – deceze Mar 19 '20 at 13:55