beginner question, function returning object.

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Thu Feb 7 03:25:12 EST 2008


On Thu, 07 Feb 2008 19:14:54 +1100, bambam wrote:

> I started with ths:
>  ------------------------------
> def open_pipe():
>     pipe=PIPE()
>     print pipe
>     return pipe
> 
>  pipe=open_pipe()
> pipe.parent = self.parent
> print pipe
>  ------------------------------
>  It didn't do what I wanted: when I printed the pipe the second time it was
>  not the same object as the first time.

Please post actual minimal code that reproduces the problem and a better
description of what you get and what you expected instead.

What is `PIPE` and where does `self` come from?  What are the too
``print``\s printing that makes you think `pipe` isn't bound to the same
object?

>  So I changed it to this:
> def open_pipe(pipe):
>     pipe=PIPE()
>     print pipe
> 
>  pipe = None
> open_pipe(pipe)
> pipe.parent = self.parent
> print pipe
> 
>  It still doesn't do what I wanted: I can't assign the parent property
>  because pipe type is None.

Yes because in `open_pipe()` you bind a new object to the local name
`pipe` which of course has no effect on the binding of the name `pipe` in
the callers namespace.

> I'm not sure enough of what I am doing to tell if I have another error in 
> my code causing the problem. Is either of these examples supposed to work
> as shown? Is it clear that either example is obviously wrong?

The second is wrong.  The first should work if `self` and `PIPE` are bound
to appropriate objects.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list