TransWikia.com

Python 3 REGEX .finditer: Usefulness of hex address in "callable_iterator object at 0x..."?

Stack Overflow Asked by RBV on November 18, 2021

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?

One Answer

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

Answered by jdaz on November 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP