Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define global function in Python?

Tags:

python

Is there a way to define a function to be global from within a class( or from within another function, as matter of fact)? Something similar to defining a global variable.

like image 521
flamenco Avatar asked Jan 13 '15 19:01

flamenco


People also ask

Can we declare a function as global in Python?

You can use global to declare a global function from within a class. The problem with doing that is you can not use it with a class scope so might as well declare it outside the class.

How do you make a global function?

The global Keyword Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function. To create a global variable inside a function, you can use the global keyword.

How do you define a global class in Python?

We declare a variable global by using the keyword global before a variable. All variables have the scope of the block, where they are declared and defined in. They can only be used after the point of their declaration.


Video Answer


2 Answers

Functions are added to the current namespace like any other name would be added. That means you can use the global keyword inside a function or method:

def create_global_function():     global foo     def foo(): return 'bar' 

The same applies to a class body or method:

class ClassWithGlobalFunction:     global spam     def spam(): return 'eggs'      def method(self):         global monty         def monty(): return 'python' 

with the difference that spam will be defined immediately as top-level class bodies are executed on import.

Like all uses of global you probably want to rethink the problem and find another way to solve it. You could return the function so created instead, for example.

Demo:

>>> def create_global_function(): ...     global foo ...     def foo(): return 'bar' ...  >>> foo Traceback (most recent call last):   File "<stdin>", line 1, in <module> NameError: name 'foo' is not defined >>> create_global_function() >>> foo <function foo at 0x102a0c7d0> >>> foo() 'bar' >>> class ClassWithGlobalFunction: ...     global spam ...     def spam(): return 'eggs' ...     def method(self): ...         global monty ...         def monty(): return 'python' ...  >>> spam <function spam at 0x102a0cb18> >>> spam() 'eggs' >>> monty Traceback (most recent call last):   File "<stdin>", line 1, in <module> NameError: name 'monty' is not defined >>> ClassWithGlobalFunction().method() >>> monty() 'python' 
like image 173
Martijn Pieters Avatar answered Oct 02 '22 16:10

Martijn Pieters


You can use global to declare a global function from within a class. The problem with doing that is you can not use it with a class scope so might as well declare it outside the class.

class X:   global d   def d():     print 'I might be defined in a class, but I\'m global'  >> X.d     Traceback (most recent call last):    File "<stdin>", line 1, in <module>    AttributeError: 'X' object has no attribute 'd'  >> d()  I might be defined in a class, but I'm global 
like image 36
Jasmeet Singh Avatar answered Oct 02 '22 16:10

Jasmeet Singh