deleting a parameter's name as it is passed to a function

A

Amir Michail

Hi,

I think it would be useful to delete a name of a parameter object as
the object is passed to a function:

dosomestuff(del a)

instead of

dosomestuff(a)
del a

The idea is to garbage collect the object as soon as possible, and this
may be sooner than when dosomestuff returns.

Amir
 
P

Paul Rubin

Amir Michail said:
The idea is to garbage collect the object as soon as possible, and this
may be sooner than when dosomestuff returns.

If it's a parameter to dosomestuff, then there's still a reference
until dosomestuff returns. Simply getting rid of the name only frees
a dictionary entry.
 
A

Amir Michail

Paul said:
If it's a parameter to dosomestuff, then there's still a reference
until dosomestuff returns. Simply getting rid of the name only frees
a dictionary entry.

But dosomestuff can get rid of its reference before it returns (perhaps
it has a lot more to do before it returns and so you would want to
garbage collect the parameter object as soon as possible).

Amir
 
P

Paul Rubin

Amir Michail said:
But dosomestuff can get rid of its reference before it returns (perhaps
it has a lot more to do before it returns and so you would want to
garbage collect the parameter object as soon as possible).

That would be so rare and weird that your best bet is to just pass a
boxed object in the odd circumstances where it will make any
difference. Instead of dosomestuff(x), say dosomestuff([x]). Then
dosomestuff can mutate the list:

def dosomestuff(xl):
x = xl.pop()
... do stuff with x ...
x = None # free the reference
... more stuff...
 
S

Steven D'Aprano

Hi,

I think it would be useful to delete a name of a parameter object as
the object is passed to a function:

dosomestuff(del a)

That's a horrible syntax. It would require Python to be completely
re-designed to allow statements where expressions are allowed, and it
isn't even clear what your syntax means. It looks like it means "delete
object a, and then call dosomestuff on the result returned by del".

instead of

dosomestuff(a)
del a

That is lovely and clear.
The idea is to garbage collect the object as soon as possible, and this
may be sooner than when dosomestuff returns.

What difference does that make? Why do you care when object a is garbage
collected?
 

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

Forum statistics

Threads
474,268
Messages
2,571,344
Members
48,019
Latest member
Migration_Expert

Latest Threads

Top