Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python argument passing in object oriented programming

I apologize if this was asked somewhere else, but I do not know how else to formulate this question.

I am a physicist and Python is my first object-oriented language. I love this language for its clean code, and somehow everything works as intended (by me ;).

However I have one problem, maybe it is more of a design choice, but since my object oriented programming is self-taught and very basic I am not sure which way to go.

So the question is: should I mainly pass arguments or manipulate the object data directly? Because, for instance:

class test(object):

    ...

    def dosomething(self, x, y):
        # do someting with x, y involving a lot of mathematic manipulations

    def calcit(self):
        k = self.dosomething(self.x[i], self.y[i])
        # do something else with k

produces much cleaner code than not passing x, y but passing i and writing the self explicitly every time. What do you prefer, or is this an object oriented paradigm that I am breaking?

Performance-wise this shouldn't make a difference since the arguments are passed by reference, right?

like image 802
thafritz Avatar asked Nov 22 '11 10:11

thafritz


People also ask

How do you pass an argument to a class in Python?

A function can take multiple arguments, these arguments can be objects, variables(of same or different data types) and functions. Python functions are first class objects. In the example below, a function is assigned to a variable.

What are the two methods of passing arguments to functions in Python?

By default, arguments may be passed to a Python function either by position or explicitly by keyword.

What is parameter passing in Python?

Parameter passing on the server side follows the rules for the client side. An operation returning multiple values returns them in a tuple consisting of a non- void return value, if any, followed by the out parameters in the order of declaration. An operation returning only one value simply returns the value itself.


2 Answers

should i mainly pass arguments or manipulate the object data directly

Think of objects as systems with a state. If data belongs to the state of the object, then it should be packaged in the object as a member. Otherwise, it should be passed to its methods as an argument.

In your example, what you should do depends on whether you want to dosomething on values x and y that are not members of the object. If you don't, then you can have dosomething fetch x and y from self.

Also, keep in mind that if you're not using self inside a method, then it probably shouldn't be a method at all but rather a freestanding function.

performance-wise this shouldn't make a difference since the arguments are passed by reference, right?

I wouldn't worry about performance at this point at all.

like image 122
Fred Foo Avatar answered Sep 21 '22 18:09

Fred Foo


Object paradigm is that : you pack up methods and attributes together and call them an object.

So, if you manipulate precisely one of those attributes you don't need to pass them as parameters, you SHOULD use the object's ones. If you use anything else then you got to pass it as parameters.

And nothing prevents you from getting the values of your object into another variable if it bothers you to write self every time !

To finish, your function that takes x and y as parameters should not be in your object but outside of it as an helper function if you really wanna do something like that, the reason being that there is no reason to pass your object as first parameter (even if it's implicit) if you do not use it.

and yeah performance wise it should be pretty similar !

like image 44
m09 Avatar answered Sep 20 '22 18:09

m09