what's the purpose / benefit of the hexadecimal value returned by the .finditer operation?
When you write print("add_date_iter ==", add_date_iter)
, you are simply converting the iterator object to a string and printing it. That is, <callable_iterator object at 0xb78e218c>
is the return value of the object's internal __str__
method.*
The hexadecimal address is telling you where in memory the iterator is stored. It is the same value you would get if you ran hex(id(add_date_iter))
. It is generally only useful when you're trying to figure out the internals of how Python is managing memory during a certain process, or if you want to check whether two variables are holding a reference to the same object. When comparing objects, you can think of id(a) == id(b)
as a long way of writing a is b
.
More detail...
For instance, if you had this code:
class A:
def __init__(self):
self.val = 0
a = A()
b = A()
print(id(a), id(b))
print(a is b)
b = a
print(id(a), id(b))
print(a is b)
You would get an output like this:
140665126149392 140665230088528
False
140665126149392 140665126149392
True
In the first case, even though the instance variables val
have the same value, the objects themselves are different. After writing b = a
, though, both variables now refer to the same object.
One place you can get tripped up with this is with integers, which is why you should always use ==
instead of is
unless you really know what you're doing (or you're checking is None
, since None
is actually a singleton object):
a = 5
b = 5
a is b # True
a = 300
b = 300
a is b # False
One final point: since Python has first class functions (i.e. functions are treated as objects), everything discussed above works with functions, too. Just reference the function without parentheses, like id(print)
.
* Note: If you write add_date_iter
without print
in the interactive shell, it will call the __repr__
method instead