On 1/11/06, Kent Johnson <[EMAIL PROTECTED]> wrote:
> Burge Kurt wrote:
> >
> > void get_args(int argc, char* argv[], Argument* args)
> >
> > My first question about the pointers here; how can I convert them to
> > Python?
>
> Python doesn't have pointers in the sense of direct references to
> memory. Python has very good built in support for higher-level
> structures than C has. Strings and lists are both built in.

the main reason why pointers are used in C (and C++) is so that the
called function has the ability to modify the object (since it is
passed by reference).

in Python, references to objects passed in are also "live," meaning
that if it is a mutable (modifiable) object, you can manipulate it
directly without using the asterisk/star (*).

in bernard's example, we've replaced char* argv[] as an argument to
main by doing an import of sys and using the argv attribute of the sys
module, i.e., sys.argv.  to access individual arguments, you would
replace something like:

for (int i = 0; i < argc; i++) {
        :
    ... argv[i] ...
        :
}

... with ...

for i in range(len(sys.argv)):
        :
    ... sys.argv[i] ...
        :

... or ...

for eachArg in sys.argv:
        :
    ... eachArg ...
        :


> I'll guess that Argument* args is a pointer to an array of Argument
> objects. In Python this would be represented by a list of Argument objects.

it looks like it's a set of variable arguments (varargs).  Python has
support for this too, and it's one of the rare places you will see
asterisks (*) used.

we replace "void get_args(int argc, char* argv[], Argument* args)"
with "def get_args(*args)".  (recall that argv is sys.argv and argc is
len(sys.argv).)  then to access each extra argument, you would:

for eachArg in args:
       :
    ... eachArg ...

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2006,2001
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to