Why this code is printing well?


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>

i’m not stuck, i’m testing

<In what way does your code behave incorrectly? Include ALL error messages.>

nothing wrong

<What do you expect to happen instead?>

i put ‘y’ in spam() and i ask print spam(x) and it Display 12. Why ?

```python

y = 5
x = 7

def spam(y):
eggs = 12
return eggs

print spam(x)

<do not remove the three backticks above>

#2

I’m french and its my first topic.

i hope you will be understanding / comprehensive of/in/from? my mistakes in the rules of doing a “good” topic


#3

Because that statement is printing the return value.

Recall that a function call is an expression, which yields a value, the return value of the function.

return 12

#4

x = 5
y = 7

/////

def spam(x):
eggs = 12
return eggs

print spam(x)

-> work as well

def spam(y):
eggs = 12
return eggs

print spam(x)

-> its WEIRD but work

def spam():
eggs = 12
return eggs

print spam(x)

-> DOESNT work


#5

i know how work return

i speak about the argument into “()” of spam() fonction

a good correlation beetween print and the argument work as well

a bad correlation work as weird

no correlation doesnt work as well

see my previous post with this diffentes lines of codes


#6

Recall that the parameters of a function are local variables, representations of the arguments used in the function call.

call (argument) => callee (parameter)

When the argument is a primitive type (number, string, boolean, etc.) and not data structures (lists, dictionaries) the parameter is a copy of the argument. Otherwise, the parameter is a reference to the argument.

When not setting a global within a function, we can query it only if there is not a local variable of the same name. In the event there is a local variable, it will shadow the global and make it unreachable.

x = 6
def foo (x):
    return x + 1

print foo(1)    # 2
print x         # 6

#7

they are a second variable y in my context.

i set the fonction for y

i ask print the fonction for x and it’s work

why ?


#9

y = 5
x = 6
def foo (y):
return y + 1

print foo(x) # ?
print x # 6


#10

x is set to 6. That is the value copied into the function, but with the name, y. The return value is 6 + 1, so,

print foo(x)    # 7

#11

so y is set to the value of x when we ask x in the print ?


#12

The y inside the function is not the same value as the global y. They are two separate variables in different scope. To be able to see the value of global y in the function, there cannot be a variable with the same name. That is what is meant by shadowing. It eclipses the global variable.


#13

oh so in Python we need to take care of Variable we set with the same name ?

Because in java it will maybe said : this variable has been already used.

Right ?


#14

Linters will warn us of this, but Python could care less. It does what we tell it to do.

It’s very important that you understand shadowing. It’s an important concept.

y = 7
def foo(x):
    return x + y

print foo(6)    # 13

The above function can see y in global scope so is able to add the value to x. the value we passed into the function.


#15

y = 7
def foo(y): #not same y than above !!!
return x + y

print foo(6) # 13 too ^^

Right ?


#16

Wrong. It will raise an exception since x is undefined.

>>> def foo(y):
	return x + y

>>> foo(6)
Traceback (most recent call last):
  File "<pyshell#169>", line 1, in <module>
    foo(6)
  File "<pyshell#168>", line 2, in foo
    return x + y
NameError: name 'x' is not defined
>>> 

#17

ah AH tanks you.

i think i get it and just i need to take care of a changing in a code have consequency !

so :

Global_Variable = x
Global_Variable2 = y

def foo (Local_Variable):

Definite_Variable (operator) Definite_Variable
""“with the rule : Local_Variable with the same name crushing / OverWrite the Globale_Variable in the fonction Area”""

return Result

Print foo(Global_Variable_you_want_use)

Right ?


#18
>>> y = 6
>>> x = 7
>>> def foo():
	return x * y

>>> foo()
42
>>> 

The function can see both variables in outer scope so returns a valid product. Both vairables have to exist before the function is called.

Consider…

>>> x = 7
>>> y = 6
>>> def bar():
	x, y = y, x

	
>>> bar()
Traceback (most recent call last):
  File "<pyshell#181>", line 1, in <module>
    bar()
  File "<pyshell#179>", line 2, in bar
    x, y = y, x
UnboundLocalError: local variable 'y' referenced before assignment
>>> 

Notice how the interpreter is treating x, y as local variables?

We address this by telling the parser to bind to the global variables.

>>> def bar():
	global x, y
	x, y = y, x

	
>>> x = 7
>>> y = 6
>>> bar()
>>> x, y
(6, 7)
>>> 

We can see that the swap did indeed take place. If setting a global from within a function, it must be bound to the function.


#19

x, y = (x;y) ? :open_mouth:

so x, y = y, x is a switch as well ^^

but i dont get it yet for :

x = 7
y = 6

def bar():
x, y = y, x

What means : local variable ‘y’ referenced before assignment

And for the 3rd exemple i think i understand why its work ^^

You go reach the global value and wathever if the global value is before of after the fonction, you can use in in the fonction.

Right ?


#20

its the disappearance of return who is the cause of problem ?

nop : its a probem but not the cause your are speaking.


#21

in this code :

x=10
y=5

def spam():
x=20
y=3
return x+y

print spam()

-> why the Second iteration of x is white ? whereas the second y is red ?

python question1