How to pass objects to a function?

Terry Reedy tjreedy at home.com
Thu Nov 1 22:23:47 EST 2001


"Shankar" <SNeelakantan_C at zaplet.com> wrote in message
news:4a0341c8.0111011723.53246ecd at posting.google.com...
> I am a python newbie.
OK

> How do I  pass a user defined object to a function?

By, for instance, invoking a name bound to such as object, much as in
any other language.

> if I have something like
>
> def func(session, url):
>         ...
>         ....
>
> and session is supposed to be an object of httpsession class, how
> would the interpreter know about it?

When it compiles the function, it does not.  Names, including argument
names, do not have types.  Only objects do.  When you call the
function, see below.

> I suppose all arguments are treated as strings.

NO<).   *Every* Python object is knows what type it is.  type(object)
queries the object as to its type and returns the response.  Every
argument becomes a reference to an object which the function uses to
get ahold of the object.

> so, if I invoke a method such as session.getreply() or something I
> would get an error.

If session has method getreply() taking no args, why should it?  (I
doesn't)

Terry J. Reedy






More information about the Python-list mailing list