简体   繁体   English

为什么不打印None then 7? 蟒蛇

[英]Why does this not print None then 7? Python

So I have this code and call me an idiot haha, but I cant get this to print None then 7. 所以我有这段代码,称我为白痴哈哈,但我无法打印出7。

Code: 码:

def function(parameter):
    parameter = parameter + 1
parameter = 6
print(function(parameter))
print(parameter == 7)

I need to know how to alter the variable that has the same name as the parameter in the function. 我需要知道如何更改与函数中的参数同名的变量。

Any help would be greatly appreciated, and if you don't understand the question I'd be glad to explain more. 任何帮助将不胜感激,如果您不明白这个问题,我将很乐于解释更多。

It prints None because your function() doesn't return anything. 它输出None因为您的function()不返回任何内容。

It prints False because parameter (which is currently 6 ) does not equal 7 . 因为parameter (当前为6 )不等于7所以它输出False


To alter the global variable which is being masked by the local variable, use the globals() function: 要更改被局部变量掩盖的globals()变量,请使用globals()函数:

def function(parameter):
    globals()['parameter'] += 1

Since I truly hate globals variable this is how I would do it: 由于我真的很讨厌全局变量,因此我将这样做:

def function(parameter):
    return parameter + 1
parameter = function(6)
print(parameter)
print(parameter == 7)

Output 产量

7
True

Basically, by specifying a variable with the same name as the parameter and assigning the function you can alter parameter as if it was the same variable. 基本上,通过指定一个与参数同名的变量并分配函数,您可以像更改相同变量一样更改parameter Note that this is not the case, it is not actually the same object. 注意不是这种情况,它实际上不是同一对象。 But if your goal is to use the same name it works. 但是,如果您的目标是使用相同的名称,则可以使用。

Define parameter as global and don't actually pass it as parameter: parameter定义为全局parameter ,实际上不将其作为参数传递:

def function():
    global parameter
    parameter += 1

What you can do is refer to it as a global . 可以做的就是将其称为global What will probably help you better is to use the concept of functions for what they are: provided any given input, they produce some result. 可能会更好地帮助您的是使用功能的概念:提供任何给定的输入,它们都会产生一些结果。

So, you'd probably want to use a return : 因此,您可能希望使用return

def fun(val):
  return val;

parameter = val(10)
print(parameter) # prints '10'

However, if you want to refer to the variable, in python you can do all sorts of crazy stuff, like accessing the module scope as an object and alter it's value there: 但是,如果要引用该变量,则可以在python中执行各种疯狂的工作,例如将模块作用域作为对象访问并在其中更改其值:

def fun(val):
  import sys;
  sys.modules[__name__].parameter = val

parameter = 10
fun(1)
print(parameter) # prints '1'

However, there's a reason I call this crazy, and that's because if a function has side effects, you can never tell from the outside. 但是,我之所以称其为“疯狂”是有原因的,这是因为如果函数具有副作用,则永远无法从外部分辨出来。 And that is complexity you want to avoid. 这就是您要避免的复杂性。 Because your code should be predictable as much as possible. 因为您的代码应尽可能地可预测。 For obvious reasons. 出于明显的原因。

Using globals is neither thread safe nor recommended. 使用全局变量既不是线程安全的,也不建议使用。 Looks like the cleanest solution 'd be to pass your parameter argument in a mutable container. 看起来最干净的解决方案是在可变容器中传递参数参数。 See https://docs.python.org/3/faq/programming.html#how-do-i-write-a-function-with-output-parameters-call-by-reference 请参阅https://docs.python.org/3/faq/programming.html#how-do-i-write-a-function-with-output-parameters-call-by-reference

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

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