function returns

B

Bart Nessux

When one returns x from a function, is x available in other functions or
scopes within Python? Also, does it matter ehat the type (str, int,
float, list, etc.) of x is?
 
S

steve holden

Bart said:
When one returns x from a function, is x available in other functions or
scopes within Python? Also, does it matter ehat the type (str, int,
float, list, etc.) of x is?

When opne returns x from a function then x is a reference to a value,
and as long as that reference is assigned to a name outside the function
it will be "kept alive" - in other words, the scope of a value is
dynamic, unlike the scope of names.

Different calls to the same function are quite at liberty to return
values of different types, although this would usually be considered a
somewhat perverse way to behave.

regards
Steve
 
E

Edward Diener

steve said:
Bart Nessux wrote:
Different calls to the same function are quite at liberty to return
values of different types, although this would usually be considered a
somewhat perverse way to behave.

I disagree about the last clause. One of the wonderful things about a
dynamically typed language like Python is that one can pass, under the same
parameter name, and return, different types to and from functions. I don't
say it should always be done but it sure comes in handy when one does it,
and it often simplifies code where the need to have different functions
doing almost the same thing is no longer necessary.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top