How to pass argument implicitly in Python Class function
<p>I was writing a python class and related functions. Suppose following is a class having a function that adds up all values of a numeric list and returns the sum</p>

<pre><code> alist = [2,3,7,4,7]
 MyClasss(object):
    def __init__(self):
        print("Roses are red")
    def add_list(self,thelist):
         j = 0
         for i in the list:
             j = j+i
    return j 

  a = MyClasss()
  print("sum is ",a.add_list(alist))
</code></pre>

<p>The above query will return the sum of the list just fine.</p>

<p>MY QUESTION IS:
Is it possible write this query or create a data object in which we do not have to pass the data object in the brackets instead write it something like this</p>

<pre><code> alist.add_list()
</code></pre>

<p>and get the same results. Like we see different functions are available for different data types.
Any insights on this will be highly appreciated. Thanks  </p>

<p>You can add it to <code>__init__</code> on construction</p>

<pre><code>class MyClasss(object):
    def __init__(self, theList):
        self.theList = theList
    def add_list(self):
        return sum(self.theList)

&gt;&gt;&gt; a = MyClasss([2,3,7,4,7])
&gt;&gt;&gt; print("sum is ", a.add_list())
23
&gt;&gt;&gt; print("sum is ", a.add_list())
23
</code></pre>

<p>Another way to do this is create a partial function from <code>functools.partial</code> and call that:</p>

<pre><code>&gt;&gt;&gt; import functools as ft
&gt;&gt;&gt; a = MyClasss()
&gt;&gt;&gt; a.add_list = ft.partial(a.add_list, [2,3,7,4,7])
&gt;&gt;&gt; a.add_list()
23
</code></pre>

python function class