jim at bizcomputinginc.com
Fri Nov 2 16:35:55 CET 2007
Bjoern Schliessmann wrote:
> 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".
This sounds like an issue of terminology. I understand that I don't
declare variables like I would in C or Java, but that they are
implicitly declared via the first assignment. And the define objects
and bind a name to them makes no sense to me. I can only assume that if
I say: my_file = open( ... the "techy" explaination is that the open
function defines a file object and binds the name my_file to it. To me,
it's easier to say that the open function creates a file object and
assigns a reference to the my_file variable. If my_file does not exist,
it is created, if my_file does exist, prior to the assignment, the type
of my_file is checked to ensure type safety.
You state that globals need to be defined before you can access them
using "global", so, now the question is, how to define in the global
scope. I guess I'm too function driven as a programmer. I have a
function which opens a file. I have a bunch of other functions which
access that opened file. Traditionally, I would declare the file_handle
var as a global, then my function which opens the file can assign the
file_handle, and all the functions that access the file can also access
the file_handle. Since Python you do not declare variables, and from
what I've read so far, Python is type strict, I don't know how I would
outside a function create my file_var without actually opening my file
outside a function.
>> 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.
You misinterpreted my question, I understand the difference between a
function and a variable and the difference between a field and a method.
What I didn't understand is that if a module is an object, then a
function is a method of the module, so, why confuse things and call a
chunk of functionalized code a function when it is in a module, but call
it a method when it is part of an object. IMHO, if a module is an
object then it is a whole lot less confusing to call a function
definition a method definition, unless there is a real distinction
between a function and a method.
> 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"
> ['__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']
> ['__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.
K, then that gives further argument that making a distinction between
the term function and method, or the term field and variable is just an
exercise in confusion.
> BTW, don't confuse "class" and "object". Classes also are objects.
I'm a long time Java programmer, I understand that distinction.
>>>> 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', [...]]
More information about the Python-list