Python method calls in constructor and variable naming conventions inside a class
<p>I try to process some data in Python and I defined a class for a sub-type of data. You can find a very simplified version of the class definition below. </p>

<pre><code>class MyDataClass(object):
    def __init__(self, input1, input2, input3):
        """
        input1 and input2 are a 1D-array
        input3 is a 2D-array
        """
        self._x_value = None      # int
        self._y_value = None      # int
        self.data_array_1 = None  # 2D array
        self.data_array_2 = None  # 1D array

        self.set_data(input1, input2, input3)

    def set_data(self, input1, input2, input3):

        self._x_value, self._y_value = self.get_x_and_y_value(input1, input2)
        self.data_array_1 = self.get_data_array_1(input1)
        self.data_array_2 = self.get_data_array_2(input3)


    @staticmethod
    def get_x_and_y_value(input1, input2):
        # do some stuff
        return x_value, y_value

    def get_data_array_1(self, input1):
        # do some stuff
        return input1[self._x_value:self._y_value + 1]

    def get_data_array_2(self, input3):
        q = self.data_array_1 - input3[self._x_value:self._y_value + 1, :]
        return np.linalg.norm(q, axis=1)
</code></pre>

<p>I'm trying to follow the '<a href="https://www.python.org/dev/peps/pep-0020/" rel="nofollow">Zen of Python</a>' and thereby to write beautiful code. I'm quite sceptic, whether the class definition above is a good pratice or not. While I was thinking about alternatives I came up with the following questions, to which I would like to kindly get your opinions and suggestions.</p>

<ol>
<li><p>Does it make sense to define ''get'' and ''set'' methods? </p>

<p><em>IMHO, as the resulting data will be used several times (in several plots and computation routines), it is more convenient to create and store them once. Hence, I calculate the data arrays once in the constructor.</em></p>

<p><em>I do not deal with huge amount of data and therefore processing takes not more than a second, however I cannot estimate its potential implications on RAM if someone would use the same procedure for huge data.</em></p></li>
</ol>

<hr>

<ol start="2">
<li><p>Should I put the function <code>get_x_and_y_value()</code> out of the class scope and convert static method to a function?</p>

<p><em>As the method is only called inside the class definition, it is better to use it as a static method. If I should define it as a function, should I put all the lines relevant to this class inside a script and create a module of it?</em></p></li>
</ol>

<hr>

<ol start="3">
<li><p>The argument naming of the function <code>get_x_and_y_value()</code> are the same as <code>__init__</code> method. Should I change it?</p>

<p><em>It would ease refactoring but could confuse others who read it.</em></p></li>
</ol>

<p>In Python, you do not need getter and setter functions. Use properties instead. This is why you can access attributes directly in Python, unlike other languages like Java where you absolutely need to use getters and setters and to protect your attributes.</p>

<p>Consider the following example of a <code>Circle</code> class. Because we can use the <code>@property</code> decorator, we don't need getter and setter functions like other languages do. This is the Pythonic answer.</p>

<p>This should address all of your questions.</p>

<pre><code>class Circle(object):
    def __init__(self, radius):
        self.radius = radius
        self.x = 0
        self.y = 0

    @property
    def diameter(self):
        return self.radius * 2

    @diameter.setter
    def diameter(self, value):
        self.radius = value / 2

    @property
    def xy(self):
        return (self.x, self.y)

    @xy.setter
    def xy(self, xy_pair):
        self.x, self.y = xy_pair


&gt;&gt;&gt; c = Circle(radius=10)
&gt;&gt;&gt; c.radius
10
&gt;&gt;&gt; c.diameter
20
&gt;&gt;&gt; c.diameter = 10
&gt;&gt;&gt; c.radius
5.0
&gt;&gt;&gt; c.xy
(0, 0)
&gt;&gt;&gt; c.xy = (10, 20)
&gt;&gt;&gt; c.x
10
&gt;&gt;&gt; c.y
20
</code></pre>

python