Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access a function variable outside the function without using "global"

I am trying to access a local function variable outside the function in Python. So, for example,

bye = '' def hi():     global bye     something     something     bye = 5     sigh = 10  hi() print bye 

The above works fine as it should. Since I want to find out if I can access bye outside hi() without using global bye, I tried:

def hi():     something     something     bye = 5      sigh = 10     return  hi() x = hi() print x.bye  

The above gives AttributeError: 'NoneType' object has no attribute 'bye'.

Then, I tried:

def hi():     something     something     bye = 5     sigh = 10     return bye  hi() x = hi() print x.bye 

This time it doesn't give even an error.

So, is there a way to access a local function variable (bye) outside its function (hi()) without using globals and without printing out variable sigh as well? (Question was edited to include sigh after @hcwhsa 's comment below.

like image 319
askance Avatar asked Oct 11 '13 19:10

askance


People also ask

How do you access a variable outside a function?

To access a variable outside a function in JavaScript make your variable accessible from outside the function. First, declare it outside the function, then use it inside the function. You can't access variables declared inside a function from outside a function.

How do you use the function variable outside the function in Python?

If you want to assign a value to a name defined outside the function, then you have to tell Python that the name is not local, but it is global. We do this using the global statement. It is impossible to assign a value to a variable defined outside a function without the global statement.

How do you access a variable in a function in Python?

The variables that are defined inside the methods can be accessed within that method only by simply using the variable name. Example – var_name. If you want to use that variable outside the method or class, you have to declared that variable as a global.

Can you access the variables defined inside a function outside its body?

variables declared inside a function can only be accessed inside that function. think of each function as a box and the lid can only be opened from inside: the function can grab what's outside or put something out, but you can't reach in its box.


1 Answers

You could do something along these lines (which worked in both Python v2.7.17 and v3.8.1 when I tested it/them):

def hi():     # other code...     hi.bye = 42  # Create function attribute.     sigh = 10  hi() print(hi.bye)  # -> 42 

Functions are objects in Python and can have arbitrary attributes assigned to them.

If you're going to be doing this kind of thing often, you could implement something more generic by creating a function decorator that adds a this argument to each call to the decorated function.

This additional argument will give functions a way to reference themselves without needing to explicitly embed (hardcode) their name into the rest of the definition and is similar to the instance argument that class methods automatically receive as their first argument which is usually named self — I picked something different to avoid confusion, but like the self argument, it can be named whatever you wish.

Here's an example of that approach:

def add_this_arg(func):     def wrapped(*args, **kwargs):         return func(wrapped, *args, **kwargs)     return wrapped  @add_this_arg def hi(this, that):     # other code...     this.bye = 2 * that  # Create function attribute.     sigh = 10  hi(21) print(hi.bye)  # -> 42 

Note

This doesn't work for class methods. Just use the instance argument, named self by convention, that's already passed to methods instead of the method's name. You can reference class-level attributes through type(self). See Function's attributes when in a class.

like image 174
martineau Avatar answered Sep 22 '22 13:09

martineau