简体   繁体   English

Python代码调试

[英]Python code debugging

I'm wondering what's the problem with this piece of code: 我想知道这段代码有什么问题:

list_=[]

def inicialize_list_(list_):
    list_=[]
    return list_

def create_list_(list_):

   list_=inicialize_list_(list_)

   i=0

   for num in range(8):
       list_.append(num)
       i=i+1

   return list_

create_list_(list_)

print list_

create_list_(list_)

print list_

This is the output I'm getting: 这是我得到的输出:

[]
[]

But this is the expected output: 但这是预期的输出:

[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7]

You're not assigning the return value of create_list_ back to list_ . 您没有将create_list_的返回值分配回list_

If you write: 如果您写:

list_ = create_list_(list_)

It'll work. 会的

I have the impression there is a fundamental missunderstanding how function arguments and variables work (forgive me if I am wrong). 我的印象是对函数参数和变量的工作原理有一个根本的误解(如果我做错了,请原谅我)。

I try to explain: 我尝试解释一下:

first, you define a global variable "list_" and initialize it to an empty list. 首先,定义一个全局变量“ list_”,并将其初始化为一个空列表。 So far. 至今。 But this is a completely different variable from those use in the functions. 但这是与函数中使用的变量完全不同的变量。

Basically, if you define a function with an argument list like 基本上,如果您定义一个带有参数列表的函数,例如

def f(arg):
    ...

Any value passed to the function can be referenced inside f using the name arg. 可以使用名称arg在f内部引用传递给函数的任何值。

The argument arg is a placeholder for any value you pass to the function when you invoke it by calling it like 参数arg是占位符,表示在调用函数时通过以下方式传递给函数的任何值:

some_var = f("whatever")

This will pass the sting "whatever" to f. 这会将“无论如何”项传递给f。 Inside f, the local variable arg will then have the value "whatever" just for this invocation. 在f内,局部变量arg将仅针对此调用具有值“ whatever”。 Another infocation of f may be f(42), where arg will have the value 42. f的另一个信息可能是f(42),其中arg的值为42。

So, arg is only accessible locally (and valid) inside f (that's why it's called a local variable). 因此,只能在f内部本地(且有效)访问arg(这就是为什么将其称为局部变量)。 Any change to arg will also be just local; 对arg的任何更改也将只是本地的; arg does not even exist outside f. arg甚至不存在于f之外。

Even worse, if you have a global variable (defined outside f) with the same name (that's what you did with list_), the local variable "shadows" the global variable of the same name (which is bad practice btw.). 更糟糕的是,如果您有一个具有相同名称的全局变量(在f之外定义)(这就是使用list_所做的事情),则局部变量会“遮盖”具有相同名称的全局变量(顺便说一句)。

So, the argument-list in a function definition does not list the variables which are accessed inside the function, but are basically just some placeholders. 因此,函数定义中的参数列表不会列出在函数内部访问的变量,而基本上只是一些占位符。

To output a local value to the outer world, a function has to return a value. 为了将局部值输出到外部世界,函数必须返回一个值。 That is also called the function result . 这也称为函数结果 Python (like C/C++, ...) the keyword "return" for this: Python(例如C / C ++,...)为此使用关键字“ return”:

def square(x):
    return x * x

This will take any numerical value and return the square of it: 这将采用任何数值并返回其平方:

>>> square(2)
4
>>> square(8)
64
...

A good way to try this is to play a little with the Python interactive console. 尝试此操作的一种好方法是在Python交互式控制台上进行一些操作。 You can define functions and type any kind of code there as if you just type it into en editor and run the file through the interpreter. 您可以在其中定义函数并键入任何类型的代码,就像您只是将其键入en编辑器并通过解释器运行文件一样。

Hope that gives you the right direction. 希望能给您正确的方向。 This is actually not Python-specific, but very basic programming techniques which are definitively required to be understood well for all programming tasks. 实际上,这不是特定于Python的,而是非常基本的编程技术,对于所有编程任务,都必须明确地理解它们。

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

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