beginner question, function returning object.

B

bambam

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

bambam

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

Marc 'BlackJack' Rintsch

I started with ths:
------------------------------
def open_pipe():
pipe=PIPE()
print pipe
return pipe

pipe=open_pipe()
pipe.parent = self.parent
print pipe

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
 
S

Steven D'Aprano

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

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

bambam

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top