Py_single_input and the side-effects...

M

moerchendiser2k3

Hi at all,

I have a small problem with Py_single_input, that I dont really know
what it
actually does.

I created my own interactive interpreter loop and when I create
objects like

p = TestObject()

this instance is just deleted on Py_Finalize() even I delete the
entire console scope long time before. It seems that Py_single_input
stores the references somewhere else. Could anyone explain me a little
bit what is actually going on here?

Thanks a lot!
Cheers, moerchendiser2k3
 
C

Carl Banks

Hi, any idea?

Python doesn't guarantee that objects will be deleted at a specific
time. There are different reasons why an object might not be deleted.

In command line mode Python keeps a reference to the most recent
result. I don't know if it happens in general for any
Py_single_input, but you could try to execute a few dummy commands
with Py_single_input to see if it helps. But if you're relying on
that behavior you really need to consider rethinking your problem.

The only foolproof way to ensure that an object has been finalized is
to do it manually (i.e., provide a finalize method to releases
resources).


Carl Banks
 
M

moerchendiser2k3

Hi Carl,

thanks for your help!!
The only foolproof way to ensure that an object has been finalized is
to do it manually (i.e., provide a finalize method to releases
resources).

Yes, you are right, thats what I thought, too. So I wanted to
manually delete the reference, but browsing the sources, seems that
I cant really free it, because its wrapped somewhere
in Py_Finalize() and PyGC_Collect and Py_FreeModules. Any ideas?

Thanks a lot!

Cheers, moerchendiser2k3
 
M

moerchendiser2k3

Hi Carl,

you are right, Python still holds the last
reference. I just set a dummy and thats it :)

Can you tell me where did you get the information from?

Thanks!
 
M

Mark Lawrence


I have always believed that the _ variable is only available
interactively because of this comment in the tutorial:-
"In interactive mode, the last printed expression is assigned to the
variable _. This means that when you are using Python as a desk
calculator, it is somewhat easier to continue calculations, for example:
...."

However a thread from IIRC a couple of days back used _ to say "I'm not
interested in you, I'm throwing you away" in a list comprehension or
whatever. I've tried this in a script this evening and it works perfectly.

print 'total', sum(amount for _, amount in outputs)

Where is the use of _ in a script documented, I've searched all over and
can't find it, guess I don't have the Midas touch with google? :)

Kindest regards.

Mark Lawrence.
 
S

Stephen Hansen

Where is the use of _ in a script documented, I've searched all over and
can't find it, guess I don't have the Midas touch with google? :)

Its purely a convention, and one that crosses language-bounds, and isn't
entirely universal even given that.

It just means 'placeholder that I care naught for'; its not a feature,
there's no code or any /ability/ to use it. Its the same as any other
variable name.

It just happens to be a name that conveys no meaning while being short
and visually distinct: except to say you don't care about what value
ends up there, consider it thrown away.

That the interactive interpreter happens to store the last value in a
variable of the same name doesn't really mean anything.

--

Stephen Hansen
... Also: Ixokai
... Mail: me+list/python (AT) ixokai (DOT) io
... Blog: http://meh.ixokai.io/


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)

iQEcBAEBAgAGBQJMGpjbAAoJEKcbwptVWx/lrnQH/i5KUQ8TFVWEX0D8DjhXuIPV
FXObw44V/xYpwhbCbRcrpTZdu7gE8As5YjEb55d6XDnLlo1nUWuLzPTJPoPtH0yj
ivpAq2hP1irB1EpVEdMnzbtQo16PF06U3P7cVG6G7ez1z/fM4ypXRhXcwq36bOKS
7WU0wmh0wwEG6dZikXYMCB3fYNP7SnIPYiHj+YaB/R5HjAvVrAbr1SIy+95JwcVI
zL4xGxKJJmw7EQ/rk6q6MzPC0s5ULpPckkj5KfqRCaCENxjzP+Bvw7nhJvZGyo76
ELM/ZcS0hl8XvFb6jMxcdKT0pRY3YfGN/VW+v/SSxsmj3XJjiwz0jBlH4Gu14xE=
=axVT
-----END PGP SIGNATURE-----
 
M

Mark Lawrence

Its purely a convention, and one that crosses language-bounds, and isn't
entirely universal even given that.

It just means 'placeholder that I care naught for'; its not a feature,
there's no code or any /ability/ to use it. Its the same as any other
variable name.

It just happens to be a name that conveys no meaning while being short
and visually distinct: except to say you don't care about what value
ends up there, consider it thrown away.

That the interactive interpreter happens to store the last value in a
variable of the same name doesn't really mean anything.

Thanks Stephen, don't think I'll lose any sleep over it then. :)

Cheers.

Mark Lawrence.
 

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
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top