PYTHON Superglobals

is this article helpful?
|
Python replacement for PHP's Superglobals [ edit | history ]
Python provides some "builtins," which are variables that are always available in all scopes.

For example, in this code:


bunch_of_numbers = range(3)


range is provided by the builtins system.

Note that since Python is a general purpose language, the list of builtins is small, and none of them is particularly web-specific.

You can find a list of Python builtin functions here: http://docs.python.org/2/library/functions.html

A warning: don't "shadow" builtins

In the previous example, we used the builtin function called "range" to generate a list. But you can actually write code so that range(3) does something else! Check out the following:


range = 45


If you write that in your Python file, suddenly if you try to call the range() function, you'll discover Python complains it is no longer a function.

This limitation (also known as "shadowing" a builtin) only applies within the function or module you write it in, so the badness is limited. It might seem like a silly thing to do, but it's a very common basic Python mistake.

You can read more about it here: http://adam-bernier.appspot.com/post/34001/shadowing-builtins

ADVANCED and BAD code: How to emulate PHP superglobals

(Please do not actually use this code anywhere!)

You can, if you really want to, create new superglobal variables in Python.

Let's say you want to create a new global variable with the GET data. Normally, there is no variable called GET:


print GET # NOTE: This will fail with an error


But if you employ the following hack, and you are running your web app within Django, you'll end up with a GET superglobal:


__builtins__['GET'] = request.GET


But please never ever do that.

PHP Superglobals

PHP original manual for Superglobals [ show | php.net ]

Superglobals

SuperglobalsSuperglobals are built-in variables that are always available in all scopes

Description

Several predefined variables in PHP are "superglobals", which means they are available in all scopes throughout a script. There is no need to do global $variable; to access them within functions or methods.

These superglobal variables are:

Changelog

Version Description
4.1.0 Superglobals were introduced to PHP.

Notes

Note: Variable availability
By default, all of the superglobals are available but there are directives that affect this availability. For further information, refer to the documentation for variables_order.

Note: Dealing with register_globals
If the deprecated register_globals directive is set to on then the variables within will also be made available in the global scope of the script. For example, $_POST['foo'] would also exist as $foo.
For related information, see the FAQ titled "How does register_globals affect me?"

Note: Variable variables
Superglobals cannot be used as variable variables inside functions or class methods.