Jim Hendricks a écrit : > New to python, programming in 15 or so langs for 24 years. > > Couple of questions the tuts I've looked at don't explain: > > 1) global vars - python sets scope to the block a var is declared (1st > set), I see the global keyword that allows access to global vars in a > function, what I'm not clear on is does that global need to be declared > in the global scope, or, when 1st set in a function where it is listed > as a global, does that then declare the necessary global.
Didn't you try by yourself ? Would have been faster than writing down your question... Python 2.5.1 (r251:54863, May 2 2007, 16:56:35) [GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> def toto(): ... global p ... p = 42 ... >>> p Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'p' is not defined >>> toto() >>> p 42 >>> Anyway, remember that in Python, 'global' really means 'module level'. > 2) Everything is an object. So then, why the distinction between > functions/variables and fields/methods. If a module is an object, would > not every function be a method of that module functions are *instance* attributes of the modules (obviously, since each module is a distinct instance of class 'module'...), and when functions are instance attributes of an object, the lookup mechanism doesn't turn them into methods. > and every variable be a > field of that module? There's nothing like a 'field' in Python - only attributes. And module-level (so-called 'global') vars *are* attributes of the module. So from 'the outside' (ie: from the importing code), a module is just an instance of class module, with it's own set of attributes - some of them callables (functions and classes), some other not. HTH -- http://mail.python.org/mailman/listinfo/python-list