简体   繁体   English

将 arguments 传递给 function(Python,OOP,函数)

[英]Passing arguments to the function (Python, OOP, functions)

So here we are passing 2 arguments to the function: 1) a just created object from the class ( counter ) and 2) a number ( 0 ). 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:为什么“数字”变量不增加,如果 function 清楚地说:

num += 1

??? ???

Every time the function is called the value is assigned to a new variable called num .每次调用 function 时,都会将值分配给一个名为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.因此,您必须从增量 function 返回number的值并将其分配给 number 变量。

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 Output

counter is 100, number of times is 100

The best way is to add increment function in the Counter class.最好的方法是在计数器 class 中添加增量 function。 But If you want to do this with function this way works fine.但是如果你想用 function 做到这一点,这种方式工作正常。

Python passes parameters by object reference, which results basically in having references to mutable types and values for immutable types. Python 通过 object 引用传递参数,这基本上导致引用可变类型和不可变类型的值。

Counter is a mutable class that you have created, whereas number is an integer (immutable). Counter是您创建的可变 class ,而number是 integer (不可变)。

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 .如其他答案和评论中所述,不可变的 integer 被覆盖,但由于它是一个局部变量,因此您无法在 function 之外看到这些更改,除非您从increment中返回num的值。

Alternatively, you could make number a class variable of Counter .或者,您可以将number设为Counter的 class 变量。 Hereby, you could track how many times any instance of Counter has ever increased the count:因此,您可以跟踪任何Counter实例增加计数的次数:

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: Output:

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

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.这是因为每当您将参数传递给 python 中的 function 时,您使用的是该参数的副本,因此写入n += 1不会影响 ZC1C425268E68385D1AB5074C14A 之外的实际变量 n。 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循环中,您可以这样做:

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.那应该行得通。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM