Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Passing self to class methods or arguments

Tags:

python

oop

I am working on a python project with classes that have quite a few parameters and methods. In order to reduce complexity, I have been writing the methods as such:

def foo(self):
    return self.parameter1 * self.parameter2

Would it be better practice to explicitly pass the parameters?

def foo(self, parameter1, parameter2):
    return parameter1 * parameter2

This comes up because I have found it difficult to test the functions in the class without testing the entire class.

like image 645
self.username Avatar asked Jan 13 '17 06:01

self.username


People also ask

Do I need to pass self as an argument Python?

Self is the first argument to be passed in Constructor and Instance Method. Self must be provided as a First parameter to the Instance method and constructor. If you don't provide it, it will cause an error.

Can class method access self in Python?

Class methods don't need a class instance. They can't access the instance ( self ) but they have access to the class itself via cls . Static methods don't have access to cls or self .

Is self automatically passed in Python?

Use self when: you define an instance method, since it is passed automatically as the first parameter when the method is called; you reference a class or an instance attribute from inside an instance method; you want to refer to instance variables and methods from other instance methods.

Do all Python class methods need self?

self is the first method of every Python class When you define a class in Python, every method that you define, must accept that instance as its first argument (called self by convention). The self variable points to the instance of the class that you're working with.


2 Answers

Your question makes some assumptions that are not consistent with OO design.

If parameter1 and parameter2 are not intrinsic properties of the object represented by self, then they need to be passed. If however, they are intrinsic properties of self, then they should have already been associated with self and do not need to be passed.

One major point of OO design, and objects in general, is to explicitly associate the data describing the object and the methods to work on the object together.

Answer:

Use a self reference for anything that is intrinsic to the object, and pass as a parameter those values which are not.

like image 124
Stephen Rauch Avatar answered Sep 29 '22 07:09

Stephen Rauch


In order to reduce complexity, I have been writing the methods as such

The assumption of this purpose is wrong, self(instance variable) is supposed to be used if you define an instance method.

According to pylint: method_could_be_a_function: If a function could be run without self(instance variable), it should be an individual function, not an instance method.

For example:

class A(object):

    def __init__(self):
        self.some_instance_var

    def some_instance_method(self):
        #code here is suggested to be related to self(instance variable), eg:
        return self.some_instance_var

This one would be proper to put into class as an instance method.

class A(object):

    ...

    def foo(self):
        return self.parameter1 * self.parameter2

And this one should be a normal function, not an instance method. Because the content of function is not related to self(instance variable).

def foo(self, parameter1, parameter2):
    return parameter1 * parameter2
like image 25
Kir Chou Avatar answered Sep 29 '22 07:09

Kir Chou