[issue43380] Assigning function parameter to class attribute by the same name

2021-03-03 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

Looking at the disassembly of the demo() function also shows differences 
between the B and C classes.

I seem to recall discussion about this on, maybe, the Python-Dev list. I think 
resolving this will probably have to wait on a re-design of the exact scoping 
rules with respect to classes inside functions.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43380] Assigning function parameter to class attribute by the same name

2021-03-03 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

Here's an example that shows what is going on:


def demo():
a = 1
class B:
x = a
print(B.x)  # Okay.

class C:
x = a  # Fails.
if False:
a = None
print(C.x)


If you run that, B.x is printed (1) but assigning to C.x fails:

>>> demo()
1
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 7, in demo
  File "", line 8, in C
NameError: name 'a' is not defined


The reason is that inside a function, assignment to a name makes it a local. 
This interacts oddly with class scope.

By the way, I get the same results with this all the way back to Python 2.4. (I 
don't have older versions to test.) So this has existed for a very long time.

--
nosy: +steven.daprano

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43380] Assigning function parameter to class attribute by the same name

2021-03-02 Thread jennydaman


jennydaman  added the comment:

Yes sorry that was a typo

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43380] Assigning function parameter to class attribute by the same name

2021-03-02 Thread jennydaman


jennydaman  added the comment:

# Example

Consider these three examples, which are theoretically identical

```
a = 4

class A:
a = a

print(A.a)

def createB(b):
class B:
z = b
print(B.z)

createB(5)

def createD(d):
class D:
d = d
print(D.d)

createD(6)
```

## Expected Output

```
4
5
6
```

## Actual Output

```
4
5
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in createD
  File "", line 3, in D
NameError: name 'd' is not defined
```

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43380] Assigning function parameter to class attribute by the same name

2021-03-02 Thread Eric V. Smith


Eric V. Smith  added the comment:

Was 
def createD(D):

supposed to be:
def createD(d):

?

Not that that changes your problem. I just want to understand the exact issue.

--
nosy: +eric.smith

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43380] Assigning function parameter to class attribute by the same name

2021-03-02 Thread jennydaman


New submission from jennydaman :

# Example

Consider these three examples, which are theoretically identical

```
a = 4

class A:
a = a

print(A.a)

def createB(b):
class B:
z = b
print(B.z)

createB(5)

def createD(D):
class D:
d = d
print(D.d)

createD(6)
```

## Expected Output

```
4
5
6
```

## Actual Output

```
4
5
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in createD
  File "", line 3, in D
NameError: name 'd' is not defined
```

--
components: Interpreter Core
messages: 387987
nosy: jennydaman
priority: normal
severity: normal
status: open
title: Assigning function parameter to class attribute by the same name
type: behavior
versions: Python 3.10, Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com