... __enter__ = intclass X:
... passwith X() as x:
0x
... return int(*a, **kw)def myint(*a, **kw):
... __enter__ = myintclass X:
... passwith X() as x:
Here, the TypeError occurred because "self" was passed as an input
<class 'int'>X.__enter__
<function X.<lambda> at 0x...>X.__exit__
<class 'int'>x2.__enter__
<bound method X.<lambda> of <__main__.X object at 0x...>>x2.__exit__
I am writing to seek your assistance in understanding an unexpected
behavior that I encountered while using the __enter__ method. I have
provided a code snippet below to illustrate the problem:
... __enter__ = intclass X:
... __exit__ = lambda *_: None
...
... passwith X() as x:
...
0x
As you can see, the __enter__ method does not throw any exceptions and returns the output of "int()" correctly. However, one would normally expect the input parameter "self" to be passed to the function.
On the other hand, when I implemented a custom function in place of the __enter__ method, I encountered the following TypeError:
```
... return int(*a, **kw)def myint(*a, **kw):
...
... __enter__ = myintclass X:
... __exit__ = lambda *_: None
...
... passwith X() as x:
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in myint
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'X'
```
Here, the TypeError occurred because "self" was passed as an input
parameter to "myint". Can someone explain why this unexpected behavior
occurs only in the latter case?
I tested this issue on the following Python versions, and the problem persists on all of them:
- Python 3.8.10 (default, Nov 14 2022, 12:59:47) [GCC 9.4.0] on linux
- Python 3.10.10 (main, Feb 8 2023, 14:50:01) [GCC 9.4.0] on linux
- Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep 5 2022, 14:08:36) [MSC v.1933
64 bit (AMD64)] on win32
I appreciate any input or insights that you might have on this matter.
int 0
I am writing to seek your assistance in understanding an unexpected
behavior that I encountered while using the __enter__ method. I have
provided a code snippet below to illustrate the problem:
```
... __enter__ = intclass X:
... __exit__ = lambda *_: None
...
... passwith X() as x:
...
0x
```
As you can see, the __enter__ method does not throw any exceptions and >returns the output of "int()" correctly. However, one would normally expect >the input parameter "self" to be passed to the function.
On 21Apr2023 00:44, Lorenzo Catoni <l.catoni.99@gmail.com> wrote:
I am writing to seek your assistance in understanding an unexpected >behavior that I encountered while using the __enter__ method. I have >provided a code snippet below to illustrate the problem:
```expect
... __enter__ = intclass X:
... __exit__ = lambda *_: None
...
... passwith X() as x:
...
0x
```
As you can see, the __enter__ method does not throw any exceptions and >returns the output of "int()" correctly. However, one would normally
the input parameter "self" to be passed to the function.
My descriptor fu is weak, but I believe this is because `int` is not a
plain function but a type.
Consider this class definition:
class X:
x = 1
def y(self):
return "y"
When you define a class, the body of the class is run in a namespace,
and on completion, the namespace is _used_ to construct the class.
During that process, the various names are considered. Here we've got 2 names: "x" and "y".
"x" refers to an int and is just stored as a class attribute, unchanged.
"y" refers to a function, and is promoted to a descriptor of an unbound method.
So later: X.x return 1 but X.y returns a unbound method. If we make an instance:
objx = X()
then obj.x returns 1 (by not fining an "x" on "obj", but finding one on "type(obj)" i.e. the class attribute.
By contrast, obj.y returns a bound method, a function already curried
with a leading parameter "obj" (which will be "self"). There's no "y" attribute directly on "obj" but there's an unbound method on
"type(obj).y", which gets bound by saying "obj.y".
The means that what happens to a name when you define the class depends
on the typeof the value bound to the name.
A plain function gets turned into an unbound instance method, but other things are left alone.
When you went:
__enter__ = int
That's not a plain function and so "obj.__enter__" doesn't turn into a
bound method - it it just `int`.
Cheers,
Cameron Simpson <cs@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list
Dear Python Mailing List members,
I am writing to seek your assistance in understanding an unexpected
behavior that I encountered while using the __enter__ method. I have
provided a code snippet below to illustrate the problem:
```
... __enter__ = intclass X:
... __exit__ = lambda *_: None
...
... passwith X() as x:
...
0x
```
As you can see, the __enter__ method does not throw any exceptions and returns the output of "int()" correctly. However, one would normally expect the input parameter "self" to be passed to the function.
On the other hand, when I implemented a custom function in place of the __enter__ method, I encountered the following TypeError:
```
... return int(*a, **kw)def myint(*a, **kw):
...
... __enter__ = myintclass X:
... __exit__ = lambda *_: None
...
... passwith X() as x:
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in myint
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'X'
```
Here, the TypeError occurred because "self" was passed as an input
parameter to "myint". Can someone explain why this unexpected behavior
occurs only in the latter case?
def __get__(self, *args): return argsclass A:
a = A()class B: pass
class X:
<__main__.B object at 0x02C2E388>x = X()
x.b
(<__main__.X object at 0x02C2E280>, <class '__main__.X'>)x.a
Truehasattr(lambda: None, "__get__")
Falsehasattr(ord, "__get__")
Falsehasattr(int, "__get__")
Dear Python Mailing List members,
I am writing to seek your assistance in understanding an unexpected
behavior that I encountered while using the __enter__ method. I have
provided a code snippet below to illustrate the problem:
```
... __enter__ = intclass X:
... __exit__ = lambda *_: None
...
... passwith X() as x:
...
0x
```
As you can see, the __enter__ method does not throw any exceptions and returns the output of "int()" correctly. However, one would normally expect the input parameter "self" to be passed to the function.
On the other hand, when I implemented a custom function in place of the __enter__ method, I encountered the following TypeError:
```
... return int(*a, **kw)def myint(*a, **kw):
...
... __enter__ = myintclass X:
... __exit__ = lambda *_: None
...
... passwith X() as x:
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in myint
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'X'
```
Here, the TypeError occurred because "self" was passed as an input
parameter to "myint". Can someone explain why this unexpected behavior
occurs only in the latter case?
I tested this issue on the following Python versions, and the problem persists on all of them:
- Python 3.8.10 (default, Nov 14 2022, 12:59:47) [GCC 9.4.0] on linux
- Python 3.10.10 (main, Feb 8 2023, 14:50:01) [GCC 9.4.0] on linux
- Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep 5 2022, 14:08:36) [MSC v.1933
64 bit (AMD64)] on win32
I appreciate any input or insights that you might have on this matter.
Thank you for your help in advance!
Best regards,
Lorenzo Catoni
0plusone("ignore", [])
6plusone("ignore", [1,2,3])
106plusone("ignore", [1,2,3], 100)
121plusone("ignore", range(7), start=100)
Dear Python Mailing List members,
I am writing to seek your assistance in understanding an unexpected
behavior that I encountered while using the __enter__ method. I have
provided a code snippet below to illustrate the problem:
```
... __enter__ = intclass X:
... __exit__ = lambda *_: None
...
... passwith X() as x:
...
0x
```
As you can see, the __enter__ method does not throw any exceptions and returns the output of "int()" correctly. However, one would normally expect the input parameter "self" to be passed to the function.
On the other hand, when I implemented a custom function in place of the __enter__ method, I encountered the following TypeError:
```
... return int(*a, **kw)def myint(*a, **kw):
...
... __enter__ = myintclass X:
... __exit__ = lambda *_: None
...
... passwith X() as x:
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in myint
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'X'
```
Here, the TypeError occurred because "self" was passed as an input
parameter to "myint". Can someone explain why this unexpected behavior
occurs only in the latter case?
I tested this issue on the following Python versions, and the problem persists on all of them:
- Python 3.8.10 (default, Nov 14 2022, 12:59:47) [GCC 9.4.0] on linux
- Python 3.10.10 (main, Feb 8 2023, 14:50:01) [GCC 9.4.0] on linux
- Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep 5 2022, 14:08:36) [MSC v.1933
64 bit (AMD64)] on win32
I appreciate any input or insights that you might have on this matter.
Thank you for your help in advance!
Best regards,
Lorenzo Catoni
Dear Python Mailing List members,
I am writing to seek your assistance in understanding an unexpected
behavior that I encountered while using the __enter__ method. I have
provided a code snippet below to illustrate the problem:
```
... __enter__ = intclass X:
... __exit__ = lambda *_: None
...
... passwith X() as x:
...
0x
```
As you can see, the __enter__ method does not throw any exceptions and returns the output of "int()" correctly. However, one would normally expect the input parameter "self" to be passed to the function.
On the other hand, when I implemented a custom function in place of the __enter__ method, I encountered the following TypeError:
```
... return int(*a, **kw)def myint(*a, **kw):
...
... __enter__ = myintclass X:
... __exit__ = lambda *_: None
...
... passwith X() as x:
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in myint
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'X'
```
Here, the TypeError occurred because "self" was passed as an input
parameter to "myint". Can someone explain why this unexpected behavior
occurs only in the latter case?
I tested this issue on the following Python versions, and the problem persists on all of them:
- Python 3.8.10 (default, Nov 14 2022, 12:59:47) [GCC 9.4.0] on linux
- Python 3.10.10 (main, Feb 8 2023, 14:50:01) [GCC 9.4.0] on linux
- Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep 5 2022, 14:08:36) [MSC v.1933
64 bit (AMD64)] on win32
I appreciate any input or insights that you might have on this matter.
Thank you for your help in advance!
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 72:54:49 |
Calls: | 6,714 |
Calls today: | 2 |
Files: | 12,246 |
Messages: | 5,357,085 |