Click to See Complete Forum and Search --> : newbie python questions


gearjunkie
09-29-2003, 09:23 AM
I was trying to learn python and was working on a couple of the code challenges in another thread and I have a couple of reallly easy python questions that I was hoping somebody could help with.

the first is: how do you use command line arguments?
for intstance if I run:
python test.py 'hello'
what is the variable that just stored 'hello'?

the second is:
how do I use a pointer, or what is the equivalent of it in python?
I want to update variables that are outside a function without using globals. I think in c it's something like

function(*variable)

to pass the memory address instead of just the value.
how do i do this in python?
thanks,
nathan

Strike
09-29-2003, 11:01 AM
Originally posted by gearjunkie
the first is: how do you use command line arguments?
for intstance if I run:
python test.py 'hello'
what is the variable that just stored 'hello'?

sys.argv and sys.argc are the same as the argv and argc in C programming. Check those out.

the second is:
how do I use a pointer, or what is the equivalent of it in python?
I want to update variables that are outside a function without using globals. I think in c it's something like

function(*variable)

to pass the memory address instead of just the value.
how do i do this in python?

From the python tutorial:

The execution of a function introduces a new symbol table used for the local variables of the function. More precisely, all variable assignments in a function store the value in the local symbol table; whereas variable references first look in the local symbol table, then in the global symbol table, and then in the table of built-in names. Thus, global variables cannot be directly assigned a value within a function (unless named in a global statement), although they may be referenced.

The actual parameters (arguments) to a function call are introduced in the local symbol table of the called function when it is called; thus, arguments are passed using call by value (where the value is always an object reference, not the value of the object). When a function calls another function, a new local symbol table is created for that call.

gearjunkie
09-29-2003, 04:43 PM
Thanks for the info.

okay, I think I get it. But, there's got to be some way of having a function be able to change or output more than one variable, right? or am I totally wrong on this.

I guess I'm just a bit unclear still about:
The execution of a function introduces a new symbol table used for the local variables of the function. More precisely, all variable assignments in a function store the value in the local symbol table; whereas variable references first look in the local symbol table, then in the global symbol table, and then in the table of built-in names. Thus, global variables cannot be directly assigned a value within a function (unless named in a global statement), although they may be referenced.

The actual parameters (arguments) to a function call are introduced in the local symbol table of the called function when it is called; thus, arguments are passed using call by value (where the value is always an object reference, not the value of the object). When a function calls another function, a new local symbol table is created for that call.

if somebody could do a bit 'dumbification' on how variable scope works, that would be appreciated. the whole thing with symbol tables and call by value is a bit cryptic to me.
I'm sure I'll figure it out eventually, but it might save a bit of frustration.

-nathan

Strike
09-29-2003, 05:53 PM
Okay, think of it this way. There is the global symbol table (and by 'symbol table', I mean the mapping of variable names to their object references), and then each function has its own symbol table, but this symbol table inherits a COPY of the parent symbol table (in many cases, the global symbol table). So you can read the values of the variables that are in the global symbol tables, but any changes you make won't have any effect outside the function. So, there are only two ways to affect the global symbol table from a function. First is by returning a value that you bind in the global scope (e.g., foo = func(bar), binding foo to the return value of func(bar)). The only other way is to act on an object that you get from the global symbol table. This is because you are actually receiving a reference to the object (which is valid anywhere in any execution path), and not just a copy of the object and its values. Here is some example code illustrating this phenomenon:

>>> class foo: pass
...
>>> a = foo()
>>> a
<__main__.foo instance at 0x402123cc>
>>> def bar(arg):
... arg.x = 1
...
>>> a.x = 2
>>> a
<__main__.foo instance at 0x402123cc>
>>> a.x
2
>>> bar(a)
>>> a
<__main__.foo instance at 0x402123cc>
>>> a.x
1
>>>


Note how the 'x' attribute of a gets modified. That's because arg within the function call just gets set to <__main__.foo instance at 0x402123cc>. Now, if we rebound foo from within bar(), then that wouldn't change anything in the global symbol table. But, we aren't rebinding foo, we are acting on foo ... well, actually we are acting on arg, but both refer to the same object in memory so the effect is the same.

Does that make any sense?

gearjunkie
10-02-2003, 04:26 PM
Okay, I think it makes more sense now. This is the sort of thing I was trying to do:


>>> class foo: pass
...
>>> obj1 = foo()
>>> obj1
<__main__.foo instance at 0x80ad2b4>
>>> def change2(arg):
... arg.x = 1
... arg.y = 2
...
>>> obj1.x = 0
>>> obj1.y = 0
>>> obj1.x
0
>>> obj1.y
0
>>> change2(obj1)
>>> obj1.x
1
>>> obj1.y
2
>>>


I'm assuming this is one of those 'basics' of object oriented programming. I've only done some really basic C programming (comp sci I, and currently an embedded control class which is in C) so I'm not totally familiar with it, but it makes a lot more sense now.
I'm going to finish up my reading of the Python tutorial, but if you (or anyone else) have any suggestions for further reading, I'd be all ears to it.
Thanks again!
-Nathan