Main Content
Global - Tcl, PHP, Python Archive - Originally posted on "The Horse's Mouth" - 2008-09-03 00:23:43 - Graham Ellis
PHP, Python, Tcl and a number of other languages have a global keyword. And it's a misnomer in most (if not all) cases.
To the un-initiated, "Global" means "worldwide" or "shared all around". So you would think that if you declare something as global, you're going to be sharing it with the same thing everywhere else. But - sorry - that's not the case. In each of the languages I quoted, global really means "Share this with the variable of the same name in the main program" .
What does this mean?
• In Tcl, Python, PHP ... you do NOT need to declare a variable as being global in your main code, even if you want to share it with your procs (Tcl) methods (Python) or functions (PHP) - it is, in effect, automatically global if you want to declare in global in a named block of code
• If you don't want a variable within a named block of code to be shared with the same 'global' variable, you can be reassured that you will not be sharing it simply by omitting any global declaration for it in the named block
• If you want to share a variable between two named blocks of code, you can do so by declaring it as global in BOTH of them ... and this has the side effect that it will also be shared, without any declaration, by any variable that happened to have the same name in your main block of code
Some other articles
Y105 - Functions, Modules and Packages From and Import in Python - where is the module loaded from? Embedding more complex code into a named block Nesting decorators Recursion in Python - the classic example What are callbacks? Why use them? An example in Python What is the difference between a function and a method? Reading command line parameters in Python A good example of recursion - a real use in Python Python - even named code blocks are objects Multiple yields and no loops in a Python generator? Python functions - an introduction to how they work Python varables - checking existance, and call by name or by value? Exception, Lambda, Generator, Slice, Dict - examples in one Python program vargs in Python - how to call a method with unknown number of parameters Optional positional and named parameters in Python Default local - a good choice by the author of Python Static variables in Python? Python timing - when to use a list, and when to use a generator Functions are first class variables in Lua and Python Finding all the unique lines in a file, using Python or Perl Python Packages - groupings of modules. An introduction Static variables in functions - and better ways using objects Passing optional and named parameters to python methods Catching the fishes first? Passing parameters to Python functions - the options you have Returning multiple values from a function call in various languages - a comparison Using an exception to initialise a static variable in a Python function / method Python - some common questions answered in code examples Passing a variable number of parameters in to a function / method Program for reliability and efficiency - do not duplicate, but rather share and re-use Optional and named parameters to Python functions/methods Python - access to variables in the outer scope Global and Enable - two misused words! Good example of recursion in Python - analyse an RSS feed Sample code with errors in it on our web site Optional parameters to Python functions Multiple returns from a function in Python Conversion of OSI grid references to Eastings and Northings Dynamic code - Python Optional and named parameters in Python What to do with a huge crop of apples Anonymous functions (lambdas) and map in Python Sharing variables with functions, but keeping them local too - Python This article Python Script - easy examples of lots of basics Returning multiple values from a function (Perl, PHP, Python) A better alternative to cutting and pasting code Function / method parameters with * and ** in Python It's the 1st, not the 1nd 1rd or 1th. Sludge off the mountain, and Python and PHP Python - A list of methods Recursion in Python Python - function v method Dynamic functions and names - Python Do not duplicate your code Cottage industry or production line data handling methods Python modules. The distribution, The Cheese Shop and the Vaults of Parnassus. Python - block insets help with documentation Python's Generator functions Difference between import and from in Python What is a callback? Code and code maintainance efficiency Call by name v call by value Lambdas in Python Python generator functions, lambdas, and iterators Distance Learning Variable Scope P209 - Subroutines in Perl Learning to use existing classes in Perl Perl functions such as chop change their input parameters Separating groups of variables into namespaces Passing a variable number of parameters in to a function / method Fresh Perl Teaching Examples - part 2 of 3 Do not copy and paste code - there are much better ways Efficient calls to subs in Perl - avoid duplication, gain speed Romeo and Julie Seven new intermediate Perl examples Daisy the Cow and a Pint of Ginger Beer Perl - Subs, Chop v Chomp, => v , This article Calling procs in Tcl and how it compares to Perl Returning multiple values from a function (Perl, PHP, Python) A better alternative to cutting and pasting code Perl - $_ and @_ Do not duplicate your code Changing @INC - where Perl loads its modules Packages in packages in Perl Where do Perl modules load from Call by name v call by value Variable Scope H105 - Functions Coding efficiency - do not repeat yourself! Passing a variable number of parameters in to a function / method Improving your function calls (APIs) - General and PHP Adding extensions to PHP Open Source applications - callbacks Static variables and ampersands in PHP A variable number of arguments in a PHP function This article Static variables in PHP Clean my plate, but keep my wine bottle. (PHP; Static) is there a lookup function in php? Returning multiple values from a function (Perl, PHP, Python) A better alternative to cutting and pasting code PHP - static declaration Global, Superglobal, Session variables - scope and persistance in PHP A lazy programmer is a good programmer Do not duplicate your code Don't repeat code - use loops or functions Functions and commands with dangerous names Code and code maintainance efficiency Passing information into and out of PHP functions Call by name v call by value There is a function in PHP to do that Variable Scope