0

i try to find a answert about my question, but I can not find a good explanation. The code below shows, that ClassA and ClassB extends from ClassAbsract. But if I try to set the value from input to self._x python creates a new _x field at the UML diagram. In the ClassA and ClassB I just wanna set _valueA or _valueB. But here is the same, the defined variable is listed two times at the UML.

What is the mistake in my demo? I use the InteliJ PyCharm IDE

class ClassAbstract:

    _x = None

    def setXvalue(self,input):
        self._x = input + 1



class ClassA(ClassAbstract):

    _valueA = None

    def setXvalue(self,input):
        self._valueA = input + 2


class ClassB(ClassAbstract):

    _valueB = None

    def setXvalue(self,input):
        self._valueB = input + 3

UML via PyCharm

michael-mammut
  • 2,595
  • 5
  • 28
  • 46
  • 2
    don't mix class variables with instance variables. The proper way of declaring an instance variable (for reuse in other methods) is through the `__init__` method. It probably confuses pycharm (even if `self._valueA` can refer to the class variable. – Jean-François Fabre Aug 28 '17 at 14:27
  • 1
    so there's probably a bug / a limitation in Pycharm class visualizer. It is perfectly valid to access your class variable using `self.` (the alternative would be to use the class name, but if it changes, you have to change it everywhere, so I always use self, but use uppercase chars to remember that it's a class variable) – Jean-François Fabre Aug 28 '17 at 14:30
  • @Jean-FrançoisFabre First, thank you for your very fast reply. So far as I understand your comment you mean: My Code is correct, and I should use self and it looks like a "bug" in pyCharm. Right? – michael-mammut Aug 28 '17 at 14:37
  • 1
    it is correct but I don't think that your intention is to create a class variable, so your code is not correct if you want to create an instance variable :) – Jean-François Fabre Aug 28 '17 at 14:41
  • well... that means i just need to create the instance variable with `self._...` because I just need the values in the current instance, of ClassA as example. The defined varialbe below `class ClassA` is for all instances of `ClassA`available... right? I belive this is my mistake. hmmm :/ – michael-mammut Aug 28 '17 at 14:48
  • I think you nailed it :) – Jean-François Fabre Aug 28 '17 at 14:59
  • Ou yes.. i am new to python and create a small project, since 3 years ago I just developed PHP and JAVA... it is a totaly new concep... Thank you @Jean-FrançoisFabre , maybe you can post your answer here, than I will set my checkmark! ;) – michael-mammut Aug 28 '17 at 15:02

0 Answers0