[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

beginner question, function returning object.

bambam

2/7/2008 7:23:00 AM

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.

So I changed it to this:
def open_pipe():
pipe=PIPE()
print pipe
return pipe

pipe = None
pipe=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.

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?




4 Answers

bambam

2/7/2008 8:15:00 AM

0

Second try (correction)

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.

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.

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?





Marc 'BlackJack' Rintsch

2/7/2008 8:25:00 AM

0

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

Steven D'Aprano

2/7/2008 8:36:00 AM

0

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

> Second try (correction)
>
> I started with ths:
> ------------------------------
> def open_pipe():
> pipe=PIPE()
> print pipe
> return pipe

What's PIPE() do?


> pipe=open_pipe()

(Extraneous space removed.)

> pipe.parent = self.parent

What's self? What's it do? What's self.parent and what does it do?

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

How do you know? What makes you think they are different objects?



> So I changed it to this:
> def open_pipe(pipe):
> pipe=PIPE()
> print pipe
>
> pipe = None

Again with the extraneous space.

> open_pipe(pipe)

This can't possibly work. What you are doing is this:

(1) set the name 'pipe' to None
(2) call the function open_pipe() with None as the argument
(3) which reassigns the *inner* variable 'pipe' to the result of PIPE(),
but doesn't do anything to the *global* variable 'pipe'
(4) open_pipe() now returns None, which doesn't get used

So as you can see, the global 'pipe' starts off as None, and then nothing
happens to it, so it stays None.

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


Your first attempt was almost certainly the correct way to try to do what
you want. I suspect that your function PIPE() is broken.



--
Steven

bambam

2/8/2008 1:25:00 AM

0

Thank you.

So example 2 was clearly wrong, and example 1 was not clear :~).

pipe is a serial port object: when I print pipe it shows first that it is
connected to port 5, then that it is connected to port 6. I'll discard
the clearly wrong code, and concentrate on the unclear code: probably
by the time I have clarified the problem, the solution will also be clear.

Thanks again..