New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Show the address in the repr for class objects #69734
Comments
In old-style classes, the memory address of the class object is shown for both the class and for the instance. In new-style classes, only the instance shows the address. I and other Python instructors have found the memory address display to be useful and I think it should be added back: Old-style
>>> class A:
pass
>>> a = A()
>>> A
<class __main__.A at 0x10061e328>
>>> a
<__main__.A instance at 0x105292cb0> New-style >>> class A(object):
pass
>>> a = A()
>>> A
<class '__main__.A'>
>>> a
<__main__.A object at 0x105332cd0> |
Hi Raymond, I just executed the code with python 3.5 and I don't have this result: Python 3.5.0 (default, Sep 23 2015, 04:41:38)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class A: pass
...
>>> A
<class '__main__.A'>
>>> class A(object): pass
...
>>> A
<class '__main__.A'>
>>> |
In 3.x, old style classes are gone, so you get a new style class with or without '(object)'. |
I don't quite get why the memory address is helpful, but you could work around this using a custom metaclass: class IdMeta(type):
def __repr__(cls):
return super().__repr__()[:-1] + " at 0x%x>"%id(cls)
class IdInRepr(metaclass=IdMeta):pass
<class 'module.name' at address> |
For the same reason that we've found the address to helpful in other reprs, it helps people understand that classes are objects just like anything else and to know which objects are distinct. My instructors have found that this matters when teaching Python and more than one of them immediately noticed the loss of information when switching from teaching Python 2 to Python 3. |
Attaching the patch for the same. Had to update the test cases for the following tests to have this behavior as expected. test_functools |
Uploading the new patch with a new test case for the same. |
I'd probably change all instances of ".*" in the regex matches to be "0x.+" instead. For the docstrings that have "..." in them, I would probably make those " at ..." as well (although you get decide if it hinders readability too much). Other than that patch LGTM. |
Patch LGTM. Just needs doc and What's New updates. |
Kushal. If you don't mind, I would like Nofar to be able to do this patch. She's been working on it since before the sprints and was slowed down by a schoolwork crunch. |
Nofar is about to upload her patch as well (she's been working on this for a while). Perhaps the two can be compared and reconciled so that both can be credited on the commit. |
Hey Raymond, I am uploading the patch which I almost committed along with whats new update :) |
Thanks Kushal. It looks like we had a race condition ;-) I'll assign it back after Nofar's work is in and reconciled. |
Nofar said not to wait for her on this one. |
New changeset af29d89083b3 by Kushal Das in branch 'default': |
Kushal, you've beat me to it. Great work! |
Hi guys, the title of the issues is "show address in the **repR**", but the NEWS file says[1] in the **repL*, which are 2 different things ! :-) And this patch change the repR so it also affect scripts and unittests. |
I understand the reasoning here, but I want to say booh to this change in 3.6.0a2 because it breaks my tests. It used to be that type(x) returned a predictable string, and that was an easy way to verify the result types of things. Perhaps a __str__ implementation could be added that avoids the memory address? |
There is also bpo-13224 proposing to change __str__() to just return the __name__ or __qualname__ if I remember correctly. |
I'll echo what Peter said and say that this breaks 5 tests in Django's test suite which are checking error messages. If it stays, perhaps it could be added to the release notes instead of just NEWS. |
I'm also echoing this... It breaks too many tests. I filed bpo-27498. |
The NEWS file got a typo. Thanks for noticing that. |
Would there be a way to confine this to just heap types? The user defined classes are where there is the most benefit. For the built-in types, it just seems to add noise. |
Doing it only for user-defined types would still break my tests. |
+1 on rolling this back. It sounds like it hurt more than it would have helped. To satisfy my curiosity, can you post one of the tests that broke. It would be nice to see what kind of tests are depend the repr of the class. AFAICT, there was never an issue with this for old-style classes in Python2.7, so something new must be occurring in the wild. |
Thanks, let's roll it back. The reason it never was an issue for old-style classes is that they Here's a link to some test code for mypy that broke: It may be irreprehensible code but it works for Python 3.2-3.5 (and |
New changeset 4f11e6b72e8f by Benjamin Peterson in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: