Home > Software engineering >  Passing arguments to the function (Python, OOP, functions)
Passing arguments to the function (Python, OOP, functions)

Time:09-04

So here we are passing 2 arguments to the function: 1) a just created object from the class (counter) and 2) a number (0).

def increment(c, num):
    c.count  = 1
    num  = 1



class Counter:
    def __init__(self):
        self.count = 0
 
 
counter = Counter()
number = 0
 
for i in range(0, 100):
    increment(counter, number)
 
print(
    "counter is "
      str(counter.count)
      ", number of times is "
      str(number)
)

The result of the code is the following:

# counter is 100, number of times is 0

Why the 'number' variable does not increase, if the function clearly says:

num  = 1

???

CodePudding user response:

Python passes parameters by object reference, which results basically in having references to mutable types and values for immutable types.

Counter is a mutable class that you have created, whereas number is an integer (immutable).

As described in the other answers and comments, the immutable integer is overriden but since it is a local variable you cannot see these changes outside the function except if you return the value of num from increment.

Alternatively, you could make number a class variable of Counter. Hereby, you could track how many times any instance of Counter has ever increased the count:

class Counter:
    number_increases = 0

    def __init__(self):
        self.count = 0

    def increase(self):
        self.count  = 1
        Counter.number_increases  = 1


c1 = Counter()
c2 = Counter()

for i in range(10):
    c1.increase()

print(f"counters are {c1.count} and {c2.count}; "  
      f"number of times is {Counter.number_increases}")

for i in range(20):
    c2.increase()

print(f"counters are {c1.count} and {c2.count}; "  
      f"number of times is {Counter.number_increases}")

Output:

counters are 10 and 0; number of times is 10
counters are 10 and 20; number of times is 30

CodePudding user response:

Every time the function is called the value is assigned to a new variable called num. I will not modify the variable.

So, You have to return the value of number from the increment function and assign it to the number variable.

def increment(c, num):
    c.count  = 1
    return num 1



class Counter:
    def __init__(self):
        self.count = 0
 
 
counter = Counter()
number = 0
 
for i in range(0, 100):
    number = increment(counter, number)
 
print(
    "counter is "
      str(counter.count)
      ", number of times is "
      str(number)
)

Output

counter is 100, number of times is 100

The best way is to add increment function in the Counter class. But If you want to do this with function this way works fine.

CodePudding user response:

That is because whenever you pass an argument to a function in python, you are using a copy of that argument, so writing n = 1 will have no effect on the actual variable n outside of the function. You can try writing:

def increment(c, num):
    c.count  = 1
    return num 1

and in the for loop you can do like this:

for i in range(100): # range(0, 100) is the same as range(100)
                     # because 0 is the default value for start
    number = increment(counter, number)

That should work.

  • Related