hello everyone serdar yagerlap here for infoworld in this episode of smart python
i'm going to examine an often misunderstood part of python the dell keyword dell short for delete
is often thought of as a way to remove an object from memory but that's not really what it does
misunderstandings about how dell works are pretty rampant even among fairly experienced
python programmers let's start with a common example in this code i declare a variable x
and i set it to a string now what python does under the hood for an operation like this is
actually a three-step process first it creates the string object second it creates the name
x in the top level namespace for this code and third it points x at the string object
now when we issue the keyword del and then a name that name is removed from the namespace that's
currently in context so when we run this code the code will try to print x twice once before
it's been deleted and once afterwards and so to that end we see hello world for the first attempt
and then a name error for the second attempt because x doesn't exist anymore we've removed it
now from this behavior you'd be inclined to believe del x is removing the string too but
that's only somewhat right again what dell is doing is removing the name x from the namespace
that it's in and since the string only has that one name pointing to it the string is removed from
memory when the name x is removed now this isn't the same thing as dell removing the underlying
string what we're doing is erasing the name that refers to it and again because x is the only name
referring to it referring to that particular string the string vanishes from memory because
it has no more references left for it but that's not always what happens
to illustrate this here's a modified example i have two variables x and y x we set to the string
as before and y we set to x in other words both x and y are now pointing at the same string object
so first we delete x and then we try to print y and then x again now if we run this code
you'll notice both x and y have the same id they both point to the same string object in memory
and what's more y still shows us the string object even though x has been deleted and attempting to
access it will give us this error but deleting x did not delete the string because we still
have at least one other thing still pointing to it which is y if we deleted both x and y then all the
references to that string would vanish and that string would be reclaimed from memory completely
so the main reason that we use dell is to remove a name from a namespace it's not to remove objects
as such removing an object is a possible side effect of removing a name but it's not guaranteed
to happen when you remove a name so to that end when should you use dell when is it in fact useful
to remove a name entirely from a namespace well one common use for this is to remove a name from
an object since objects have their own namespaces and one example of this would be removing a
key from a dictionary sometimes we don't want a particular key value pair in a dictionary at all
and we want to ensure that it's been removed before we do something else
so in this example i have a dictionary with the key a and the value hello world if we use del
on the key rather than on the dictionary as a whole we can remove just that key value pair
so if we run this example you can see how this works before we have the dictionary with a
as its sole key after deleting a we now have an empty dictionary
any other items in that dictionary would still remain because we haven't touched them
now i need to contrast this with a different behavior i need to contrast this with
reassigning a the key a to some other value if we set a to none for instance that wouldn't
be the same thing as removing a from the dictionary entirely the key a would still
exist it would still have a value assigned to it that value would just be the object none
but deleting the key entirely removes both key and value from the dictionary completely
so to sum up the dell keyword in python is used to remove a name from a namespace it
doesn't necessarily delete the object pointed to by that name unless the deleted name happens to
be the last reference to that object so to that end it's most useful when you want to completely
remove a name rather than just assigning it to something else and one common scenario for that
is when you were removing something from an object such as a key from a dictionary or you
might want a given key to be removed entirely rather than just reassigned to something else
so that's it for this video if you liked it please leave a comment below
don't forget to subscribe to the tech talk channel on youtube and for more smart python
and more dev with serdar be sure to follow me on facebook youtube and infoworld.com