Python and OO programming (class attributes and derived classes)

I have been a Java programmer for 4.5 years. Now I have switched to Python (and the main reason is that I am now a freelancer and I work alone). I provide source code to my clients, and sometimes I have to motivate my design decisions. Now to the question. I have to support my design choice:

class Base(object):
    def foo(self):
        self.dosomethig(self.clsattr1)
        self.dosomethig(self.clsattr2)

class Derived(Base):
    clsattr1 = value1
    clsattr2 = value2

The base class must always be extended or derived. My client (a Java programmer) claims that my approach is not elegant from an OO point of view. He argues that the following approach is better:

class Base(object):
    def __init__(self, clsattr1, clsattr2):
        self.clsattr1 = clsattr1
        self.clsattr2 = clsattr2

    def foo(self):
        self.dosomethig(self.clsattr1)
        self.dosomethig(self.clsattr2)

class Derived(Base):
    def __init__(self):
        super(Derived, self).__init__(value1, value2)

I understand that the second approach is much broader than the first, but I told him that the first approach is much more convenient. I told him that I did not see any problems, but he was not convinced. Is the first approach so bad? And why?

+3
3

, . , - , .

+3

Base , , .

, , Python, .

, Base, . ? ? ? ?

, , Base .

class Base(object):
    def __init__(self, attr1=DEFAULT1, attr2=DEFAULT2):
        self.attr1 = attr1
        self.attr2 = attr2
0

The second snippet can be called "elegant" compared to Java. Not only is this almost twice as large, the use of the __ variables is a clear sign that the programmer is struggling with the language, and there are many possible errors.

If you (or your client) want Java, you know where to find it.

-5
source

Source: https://habr.com/ru/post/1786819/


All Articles