0

So I have this code and call me an idiot haha, but I cant get this to print None then 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.

Binary111
  • 149
  • 4
  • 15
  • If you *must* use it as a parameter, then you cannot, because parameters are forced to be local variables. This doesn't sound like a good idea anyway. – juanpa.arrivillaga Nov 03 '17 at 23:16
  • Optimally, you'd not want to use globals as they aren't as efficient as local variables- why not simply return `parameter + 1` and set `parameter = function(parameter)` or something along those lines? – uspectaculum Nov 03 '17 at 23:20
  • Possibly duplicate: https://stackoverflow.com/questions/10235973/modifying-global-variable-with-same-name-as-local-variable – Attie Nov 03 '17 at 23:22

5 Answers5

2

It prints None because your function() doesn't return anything.

It prints False because parameter (which is currently 6) does not equal 7.


To alter the global variable which is being masked by the local variable, use the globals() function:

def function(parameter):
    globals()['parameter'] += 1
Attie
  • 6,690
  • 2
  • 24
  • 34
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. 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.

scharette
  • 9,437
  • 8
  • 33
  • 67
0

Define parameter as global and don't actually pass it as parameter:

def function():
    global parameter
    parameter += 1
GPhilo
  • 18,519
  • 9
  • 63
  • 89
  • 1
    And change the final print to `print(parameter)`, not `print(paramater == 7)`, if you want it to actually print `7`. – ShadowRanger Nov 03 '17 at 23:22
0

What you can do is refer to it as a 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:

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:

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.

Gerard van Helden
  • 1,601
  • 10
  • 13
0

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

paperino9
  • 159
  • 1
  • 8