0

The following Python 3.5 prints add_date_iter == <callable_iterator object at 0xb78e218c>.

import re

date_added_attrs = re.compile(r'( +ADD_DATE=)("(\d+)")')

add_date_iter = date_added_attrs.finditer(test_string)
print("add_date_iter ==", add_date_iter)

So far, so good. BUT of what use is 0xb78e218c? It appears to be a hexadecimal memory or object address. Whatever it is, why / how if at all might a Python 3 program make use of it?


EDIT: My question is NOT about REGEX. The REGEX works fine. My question is, what's the purpose / benefit of the hexadecimal value returned by the .finditer operation?

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
RBV
  • 1,367
  • 1
  • 14
  • 33
  • 1
    Well what is your goal of using regex here? Can you also include sample input along with the output you want to see? – Tim Biegeleisen Jul 22 '20 at 02:57
  • The REGEX works just fine. That's not my question. My question is, why is what appears to be a hexadecimal value included as part of the callable iterator returned by the successful REGEX finditer operation? – RBV Jul 22 '20 at 03:01
  • You are trying to print the _iterator_. That's not what an iterator is used for. Again, what is your actual goal here? – Tim Biegeleisen Jul 22 '20 at 03:03
  • 2
    When you print this particular Python object, you happen to get a memory address where the iterator resides, this is all you are seeing here. – Tim Biegeleisen Jul 22 '20 at 03:05
  • Great. That's part of the answer I was looking for. So how -- if at all -- might that memory address be used? Or is it just something included for "fun"? – RBV Jul 22 '20 at 03:10
  • 1
    No, that's just part of the standard `__repr__` inherited from `object.__repr__`. It is helpful when you are maybe debugging things, but isn't supposed to be used for anything. So just check out `print(object(), object(), object())` – juanpa.arrivillaga Jul 22 '20 at 04:50

1 Answers1

2

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

jdaz
  • 5,964
  • 2
  • 22
  • 34