Skip to content

Show the address in the repr for class objects #69734

Closed

Description

@rhettinger
BPO 25548
Nosy @gvanrossum, @brettcannon, @rhettinger, @terryjreedy, @ned-deily, @petere, @vadmium, @kushaldas, @matrixise, @pppery, @timgraham, @Carreau, @Vgr255, @Shredder13
Files
  • issue25548.patch: Code and test updates for the same
  • issue25548v2.patch: Second version of the patch
  • issue25548v4.patch: with news update
  • 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:

    assignee = 'https://github.com/kushaldas'
    closed_at = <Date 2016-07-14.04:13:42.855>
    created_at = <Date 2015-11-03.18:40:11.560>
    labels = ['type-feature', 'release-blocker']
    title = 'Show the address in the repr for class objects'
    updated_at = <Date 2016-07-14.04:13:42.849>
    user = 'https://github.com/rhettinger'

    bugs.python.org fields:

    activity = <Date 2016-07-14.04:13:42.849>
    actor = 'python-dev'
    assignee = 'kushal.das'
    closed = True
    closed_date = <Date 2016-07-14.04:13:42.855>
    closer = 'python-dev'
    components = []
    creation = <Date 2015-11-03.18:40:11.560>
    creator = 'rhettinger'
    dependencies = []
    files = ['43119', '43151', '43153']
    hgrepos = []
    issue_num = 25548
    keywords = ['patch']
    message_count = 27.0
    messages = ['254012', '254017', '254232', '265883', '266038', '266963', '267110', '267112', '267113', '267119', '267121', '267122', '267225', '267286', '267344', '267403', '267704', '268760', '268763', '269075', '270262', '270296', '270313', '270314', '270342', '270345', '270360']
    nosy_count = 16.0
    nosy_names = ['gvanrossum', 'brett.cannon', 'rhettinger', 'terry.reedy', 'ned.deily', 'petere', 'python-dev', 'martin.panter', 'kushal.das', 'matrixise', 'ppperry', 'Tim.Graham', 'mbussonn', 'abarry', 'Winterflower', 'Nofar Schnider']
    pr_nums = []
    priority = 'release blocker'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue25548'
    versions = ['Python 3.6']

    Activity

    1. rhettinger commented on Nov 3, 2015

      @rhettinger
      ContributorAuthor

      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>
    2. matrixise commented on Nov 3, 2015

      @matrixise
      Member

      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'>
      >>>
    3. terryjreedy commented on Nov 6, 2015

      @terryjreedy
      Member

      In 3.x, old style classes are gone, so you get a new style class with or without '(object)'.

    4. pppery commented on May 19, 2016

      ppperymannequin
      Mannequin

      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

      IdInRepr and all its subclasses will then have the memory address in their representation:

      <class 'module.name' at address>

    5. rhettinger commented on May 22, 2016

      @rhettinger
      ContributorAuthor

      I don't quite get why the memory address is helpful

      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.

    6. kushaldas commented on Jun 2, 2016

      @kushaldas
      Member

      Attaching the patch for the same. Had to update the test cases for the following tests to have this behavior as expected.

      test_functools
      test_cmd_line_script
      test_ctypes
      test_defaultdict
      test_descr
      test_descrtut
      test_doctest
      test_generators
      test_genexps
      test_metaclass
      test_pprint
      test_reprlib
      test_statistics
      test_trace
      test_wsgiref

    7. kushaldas commented on Jun 3, 2016

      @kushaldas
      Member

      Uploading the new patch with a new test case for the same.

    8. Vgr255 commented on Jun 3, 2016

      Vgr255mannequin
      Mannequin

      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.

    9. brettcannon commented on Jun 3, 2016

      @brettcannon
      Member

      Patch LGTM. Just needs doc and What's New updates.

    10. rhettinger commented on Jun 3, 2016

      @rhettinger
      ContributorAuthor

      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.

    11. rhettinger commented on Jun 3, 2016

      @rhettinger
      ContributorAuthor

      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.

    12. kushaldas commented on Jun 3, 2016

      @kushaldas
      Member

      Hey Raymond, I am uploading the patch which I almost committed along with whats new update :)
      I am also reassigning the ticket to you so that you can decide the next steps.

    13. removed their assignment
      on Jun 3, 2016
    14. 6 remaining items

    15. Shredder13 commented on Jun 5, 2016

      Shredder13mannequin
      Mannequin

      Kushal, you've beat me to it. Great work!

    16. Carreau commented on Jun 7, 2016

      Carreaumannequin
      Mannequin

      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.

      [1] since https://hg.python.org/cpython/rev/af29d89083b3

    17. petere commented on Jun 18, 2016

      peteremannequin
      Mannequin

      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?

    18. vadmium commented on Jun 18, 2016

      @vadmium
      Member

      There is also bpo-13224 proposing to change __str__() to just return the __name__ or __qualname__ if I remember correctly.

    19. timgraham commented on Jun 22, 2016

      timgrahammannequin
      Mannequin

      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.

    20. gvanrossum commented on Jul 12, 2016

      @gvanrossum
      Member

      I'm also echoing this... It breaks too many tests. I filed bpo-27498.

    21. kushaldas commented on Jul 13, 2016

      @kushaldas
      Member

      The NEWS file got a typo. Thanks for noticing that.
      This change did require a lot of updates to the tests.
      If rest of the people agrees to revert this change, then we should go ahead and do it. Just wondering if Raymond has anything to add.

    22. rhettinger commented on Jul 13, 2016

      @rhettinger
      ContributorAuthor

      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.

    23. gvanrossum commented on Jul 13, 2016

      @gvanrossum
      Member

      Doing it only for user-defined types would still break my tests.

    24. rhettinger commented on Jul 13, 2016

      @rhettinger
      ContributorAuthor

      +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.

    25. gvanrossum commented on Jul 13, 2016

      @gvanrossum
      Member

      Thanks, let's roll it back.

      The reason it never was an issue for old-style classes is that they
      behaved like this from the start, so nobody wrote tests that depended
      on the predictability of repr(). But new-style classes have had this
      nice clean repr() since they were introduced (in 2.3?) so it's
      unsurprising that this is now depended upon.

      Here's a link to some test code for mypy that broke:
      https://github.com/python/mypy/blob/master/mypy/util.py#L22-L23

      It may be irreprehensible code but it works for Python 3.2-3.5 (and
      for new-style classes in Python 2, except mypy requires Python 3), and
      broke in 3.6.

    26. python-dev commented on Jul 14, 2016

      python-devmannequin
      Mannequin

      New changeset 4f11e6b72e8f by Benjamin Peterson in branch 'default':
      Backed out changeset af29d89083b3 (closes bpo-25548) (closes bpo-27498)
      https://hg.python.org/cpython/rev/4f11e6b72e8f

    27. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions