python newbie

Bjoern Schliessmann usenet-mail-0306.20.chr0n0ss at spamgourmet.com
Fri Nov 2 23:10:34 CET 2007


Jim Hendricks wrote:
> 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.  

I think yes, it's an issue of terminology. As mentioned I used the
terms I know from C/C++, where declaration (telling the compiler
what type it is) is included in definition (allocating memory).

http://en.wikipedia.org/wiki/Declaration_(computer_science)

> And the define objects and bind a name to them makes no sense to
> me.  

By "defining", I mean "tell the interpreter what it is", and "bind a
name to it" is standard Python terminology for, well, assignments.

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

That's what I meant.

> To me, it's easier to say that the open function creates a file
> object and assigns a reference to the my_file variable.

This is non-standard terminology, but the concept is the same.

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

It isn't at all, Python has dynamic typing. If you afterwards say 

my_file = 42

, the things Python does are

- create an int object with value 42
- unbind your file object (if there are no more names assigned to it
  it is free for garbage collection)
- bind the name my_file to the int object

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

Just write 

my_global = "ni"

somewhere in global context above the point where the function is
executed.

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

IMHO, that's strange even for "function driven" (procedural)
programming. The traditional way is:

data_file = open("/path/to/file")
read_data(data_file)
write_stuff(data_file)
data_file.close()

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

Excuse me, why shouldn't you open that file outside of a function?

If you don't like it you can also do

my_file = my_file_opening_function(parameter_1, parameter_2)

or the like.

> What I didn't understand is that if a module is an object, then
> a function is a method of the module, 

Not really; I'm sorry that I used the term "method" so sloppy
before, it just adds to the confusion :(

In Python terminology, objects aren't comprised of just fields and
methods, it is much more liberal: Python objects only
have "attributes". Attributes can be anything: An integer, a
function, a class, a class instance, ... they are just names in the
object's namespace which are bound to (= refer to) different
objects.

Python methods, OTOH, are specially wrapped function attributes that
class objects can have. The only thing that's special about them is
that if you call them from outside, they'll automatically get the
class instance or class (for class methods) you called them on as
first parameter.

Please someone correct me if I'm wrong.

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

As you saw, there _is_ a real difference between a Python function
and a Python method (this case is often referred to as "syntactic
sugar"). The terms "field" and "variable" are, as explained before,
uncommon in Python since there are really only attributes.

Regards,


Björn

-- 
BOFH excuse #307:

emissions from GSM-phones




More information about the Python-list mailing list