[New-bugs-announce] [issue23824] in-place addition of a shadowed class field

Jethro report at bugs.python.org
Tue Mar 31 16:01:23 CEST 2015

New submission from Jethro:

Look at this simple code:

    class A:
	tot = 0
	def __init__(self, a):
		self.tot += a

    x = A(3)
    print(x.tot, A.tot)

Result from print: 

    3 0

What the interpreter did was that it first resolved self.tot to be the class field tot, which resolves to 0, then it created an instance field tot to hold the result 3.

This is not correct, as one single self.tot meant two different things. Two ways to fix this: 

1. report a name undefined error (interpret self.tot as instance field)

2. increase A.tot (interpret self.tot as class field)

Clearly 1 seems more naturally to Python, even though I wished python could disallow a class field to be shadowed by instance field, which seems quite a reasonable thing to do.

components: Interpreter Core
messages: 239714
nosy: jethro
priority: normal
severity: normal
status: open
title: in-place addition of a shadowed class field
type: behavior
versions: Python 3.4

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list