I want to add a 'stack' to a class, with methods like this:
class MyClass:
class Extras:
# defs...
def __init__( self ):
# .. other init code
self.extras_stack = []
def push_extras( self, vars ):
self.extras_stack.append( Extras( vars ) if vars else None )
def pop_extras( self ):
extras = self.extras_stack.pop()
del extras
Now, this seems to work OK, but what is going on when 'del' sees 'None'? I assume it just ignores 'None', but I cannot see that in the python documentation.
Also, why can't I write:
del self.extas_stack.pop()
Since pop() returns an Extras() object (or None), it seems that that ought to work?
EDIT
Sorry again for incorrect/misleading title.
I now understand the source of my misapprehension. The reason is that 'del None' looks like a null-pointer delete in C/C++. That was why it felt wrong to me. The 'Also' part of my question was actually relevant too: The point about del is that it reduces the usage count for an object, you need the variable (which acts as a smart pointer I guess) return to do that. That's why you can't just write 'del self.extras_stack.pop()'.
I also now understand that you can't guarantee for del to be called when 'del x' occurs, which means I can't use it here.
I am sorry if this question is felt to be a duplicate, but I at least feel I have learned something here.