Python statement (deI) and python behavior

When the del statement is issued:

del var

Shouldn't it be removed from the list of a known variable, and shouldn't the python interpreter throw an "unresolved reference" error?

Or is it just just deleting the object and the absence of a name (var) not indicating anywhere? Why would such behavior be beneficial? In which cases?

Also, I'm just talking about deleting one variable. Not a list del [3] or similar.

note: I ask if this python behavior is implied in this way. And in what cases it will still be useful.

EDIT: Charles Addis gave a detailed explanation. I also admit my mistake that I was mistaken in the behavior of pycharma as an official python. Now I'm trying to use ipython along with the official interactive python shell. Despite the fact that this is my mistake, I'm glad I learned a lot about python variables along with some python debugging commands.

+4
source share
3 answers

Not sure what you are asking, so clearly what is happening ...

>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
>>> x = 10
>>> 'x' in vars()
True
>>> vars()['x']
10
>>> del x
>>> 'x' in vars()
False
>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined

, Python locals() ( vars() - dir(obj), )... , , , . , . C, NULL.

>>> x = 10
>>> def func():
...   global x
...   del x
...
>>> x
10
>>> func()
>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
>>>

:

, , (: , , - ) , 0. IMO he python, .

ID, :

Help on built-in function id in module __builtin__:

id(...)
    id(object) -> integer

    Return the identity of an object.  This is guaranteed to be unique among
    simultaneously existing objects.  (Hint: it the object memory address.)

, , , ( python , , - ruby, ).

>>> import sys
>>> x = 10     # 10 is a common value, probably exists in memory already
>>> sys.getrefcount(x)
26
>>> id(x)      # memory location of x
140266396760096
>>> y = x
>>> id(y) == id(x)
True
>>> z = 10
>>> id(z) == id(y) == id(x)
True
>>> sys.getrefcount(y)
28
>>> sys.getrefcount(z)
28
>>> del y, z
>>> sys.getrefcount(x)
26
>>> del x
>>> x = 'charlie'
>>> id(x)
4442795056
>>> y = 'charlie'
>>> z = x
>>> id(x) == id(y) == id(z)
True
>>> sys.getrefcount(x)
4
>>> sys.getrefcount(y)
4
>>> sys.getrefcount(z)
4
>>> del y
>>> del x
>>> sys.getrefcount(z)     # will be two because this line is an additional reference
2
>>> id(z)                  # pay attention to this memory location because this 
4442795056                 # is the last remaining reference to 'charlie', and
>>> del z                  # when it goes out of scope 'charlie' is removed from
>>>                        # memory.
>>> id('charlie')          # This has a different memory location because 'charlie'
4442795104                 # had to be re-created.

'x' == 10, . 10 , , - . python, , . 24 10. x = 10 25- , sys.getrefcount(x) - 26- ( ). y = 10 z = x, , , . del , 3 , 10 .

x = 'charlie', y = 'charlie' , , z = x. , . 'charlie'. , id('charlie'), , , , .

- 'charlie' 10 . 10 , . , . 'charlie' , 10 .

>>> hex(id(10))         # high address, this is on the stack
'0x7f9250c0b820'
>>> hex(id('charlie'))  # lower address, this is on the heap
'0x108cfac60
+2

python " "?

, . del -, NameError.

(var) ? ? ?

Python . "".

, - , .

- , , Python .

() del, , . ( ) ; , Python . - , .

, :

>>> x = 5
>>> y = x
>>> del x
>>> y
5

x, y , x ( 5), , ; x:

>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
+1

"del" . , .

>>> import sys
>>> x = 123456
>>> y = x               # create a second reference to the number
>>> dir()               # known variables include "x" and "y"
['__builtins__', '__doc__', '__name__', '__package__', 'sys', 'x', 'y']
>>> sys.getrefcount(x)
3
>>> del x               # remove "x" as a known variable
>>> dir()               # known variables includes only "y"
['__builtins__', '__doc__', '__name__', '__package__', 'sys', 'y']
>>> sys.getrefcount(y)  # reference count is now lower by 1
2
>>> del y               # remove "y" as a known variable
>>> dir()               # known variables no longer include "x" and "y"
['__builtins__', '__doc__', '__name__', '__package__', 'sys']
>>> x                   # unresolved variable raises a "NameError"

Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    x                  # unresolved variable raises a "NameError"
NameError: name 'x' is not defined
+1

Source: https://habr.com/ru/post/1658048/


All Articles