Skip to content

Inconsistent results with super and __getattribute__ #39013

Closed

Description

@grodr
mannequin
BPO 783528
Nosy @devdanzin
Superseder
  • bpo-14965: super() and property inheritance behavior
  • Files
  • OP_example.py: OP's example, cleaned up
  • 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 = None
    closed_at = <Date 2013-02-24.01:08:24.952>
    created_at = <Date 2003-08-05.14:19:12.000>
    labels = ['interpreter-core', 'type-bug']
    title = 'Inconsistent results with super and __getattribute__'
    updated_at = <Date 2013-02-24.01:08:24.951>
    user = 'https://bugs.python.org/grodr'

    bugs.python.org fields:

    activity = <Date 2013-02-24.01:08:24.951>
    actor = 'r.david.murray'
    assignee = 'none'
    closed = True
    closed_date = <Date 2013-02-24.01:08:24.952>
    closer = 'r.david.murray'
    components = ['Interpreter Core']
    creation = <Date 2003-08-05.14:19:12.000>
    creator = 'grodr'
    dependencies = []
    files = ['13063']
    hgrepos = []
    issue_num = 783528
    keywords = []
    message_count = 3.0
    messages = ['60365', '81870', '114254']
    nosy_count = 3.0
    nosy_names = ['grodr', 'ajaksu2', 'BreamoreBoy']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '14965'
    type = 'behavior'
    url = 'https://bugs.python.org/issue783528'
    versions = ['Python 2.7']

    Activity

    1. grodr commented on Aug 5, 2003

      grodrmannequin
      MannequinAuthor

      Consider the following scenario of property overriding:

      >>> class Test(object):
      ... 	def __init__(self, n):
      ... 		self.__n = n
      ... 	#Properties.
      ... 	def __get_n(self):
      ... 		return self.__n
      ... 	def __set_n(self, n):
      ... 		self.__n = n
      ... 	n = property(__get_n, __set_n)
      ... 	
      >>> a = Test(42)
      >>> a.n
      42
      >>> a.n = 32
      >>> a.n
      32

      Now, let us override the n property:

      >>> class Test2(Test):
      ... 	def __init__(self, n):
      ... 		super(Test2, self).__init__(n)
      ... 	#Properties.
      ... 	def __get_n(self):
      ... 		return "got ya!"
      ... 	def __set_n(self, value):
      ... 		print "No way, jose!"
      ... 	n = property(__get_n, __set_n)
      ... 
      >>> a = Test2(42)
      >>> a.n
      'got ya!'
      >>> a.n = 0
      No way, jose!
      >>> a.n
      'got ya!'
      >>> super(Test2, a).n
      42

      So far so good, super is working well with properties. But
      now consider the following inconsistencies:

      >>> super(Test2, a).__getattribute__('n')
      'got ya!'
      >>> super(Test2, a).n
      42

      Also:

      >>> super(Test, Test2).__getattribute__('n')
      <property object at 0x01103300>
      >>> super(Test, Test2).__getattribute__('n').__get__(a)
      'got ya!'
      >>> super(Test, Test2).n
      Traceback (most recent call last):
        File "<interactive input>", line 1, in ?
      AttributeError: 'super' object has no attribute 'n'

      The last one is particularly damaging, because:

      >>> super(Test2, a).n = 32
      Traceback (most recent call last):
        File "<interactive input>", line 1, in ?
      AttributeError: 'super' object has no attribute 'n'

      I think the above should work, but whatever’s the
      answer, together with the super(Test, Test2) odd
      results it does make impossible using super and setting
      properties together.

      Tested with the latest Python 2.3 in win2k.

      With my best regards,
      G. Rodrigues

      P.S: bug 729913 talks of similar issues at the end: super
      does not seem to intercept special methods.

    2. devdanzin commented on Feb 13, 2009

      devdanzinmannequin
      Mannequin

      Same behavior, is this a real issue?

      Current exception:
      TypeError: 'super' object has only read-only attributes (assign to .n)

    3. BreamoreBoy commented on Aug 18, 2010

      BreamoreBoymannequin
      Mannequin

      Closed as no response to msg81870.

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

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions