python newbie

Bjoern Schliessmann usenet-mail-0306.20.chr0n0ss at
Fri Nov 2 15:57:34 CET 2007

Please use a more informative subject next time.

Jim Hendricks wrote:
> 1) global vars - python sets scope to the block a var is declared
> (1st set),

I'm afraid not. Python only interprets the listed name(s) as

> 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,

You can't just declare in Python, you always define objects (and
bind a name to them). Yes, globals need to be defined before you
can access them using "global".

> 2) Everything is an object.  So then, why the distinction between
> functions/variables and fields/methods.  

Because they are different things to achieve different goals. One
holds data, the other does stuff.

But nonetheless, both a function and a string literal have their own
set of methods. You can look at them using "dir", a function to
show attributes (the pythonic word for fields/methods) of objects.

>>> def test():
...     print "test"
>>> dir(test)
['__call__', '__class__', '__delattr__', '__dict__', '__doc__',
'__get__', '__getattribute__', '__hash__', '__init__', '__module__',
'__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', 
'__setattr__', '__str__', 'func_closure', 'func_code', 'func_defaults', 
'func_dict', 'func_doc', 'func_globals', 'func_name']
>>> dir("honk")
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', 
'__ge__', '__getattribute__', '__getitem__', '__getnewargs__', 
'__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', 
'__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', 
'__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', 
'__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 
'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 
'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 
'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 
'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 
'title', 'translate', 'upper', 'zfill']

> If a module is an object, would not every function be a method of
> that module and every variable be a field of that module?

They are.

BTW, don't confuse "class" and "object". Classes also are objects.

>>> import os
>>> dir (os)
[[...], '__all__', '__builtins__', '__doc__', 
'__file__', '__name__', '_copy_reg', '_execvpe', '_exists', '_exit', 
'_get_exports_list', '_make_stat_result', '_make_statvfs_result', 
'_pickle_stat_result', '_pickle_statvfs_result', '_spawnvef', 
'abort', 'access', 'altsep', 'chdir', 'chmod', 'chown', 'chroot', 
'close', 'confstr', 'confstr_names', 'ctermid', 'curdir', 'defpath', 
'devnull', 'dup', 'dup2', 'environ', 'error', 'execl', 'execle', 
'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 
'extsep', 'fchdir', 'fdatasync', 'fdopen', 'fork', 'forkpty', 
'fpathconf', 'fstat', 'fstatvfs', 'fsync', 'ftruncate', [...]]


BOFH excuse #205:

Quantum dynamics are affecting the transistors

More information about the Python-list mailing list