Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

The generation of random numbers is too important to be left to chance.


devel / comp.lang.python / Re: Question regarding unexpected behavior in using __enter__ method

SubjectAuthor
o Re: Question regarding unexpected behavior in using __enter__ methodCameron Simpson

1
Re: Question regarding unexpected behavior in using __enter__ method

<mailman.139.1682054516.20750.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=27443&group=comp.lang.python#27443

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: cs@cskk.id.au (Cameron Simpson)
Newsgroups: comp.lang.python
Subject: Re: Question regarding unexpected behavior in using __enter__ method
Date: Fri, 21 Apr 2023 10:25:46 +1000
Lines: 68
Message-ID: <mailman.139.1682054516.20750.python-list@python.org>
References: <CAEGro7UrWuNKwBV8HuW8geMk+NM4T=4_DRfztT1p0Md1EcTW3A@mail.gmail.com>
<ZEHYCjlZyl0Eeelq@cskk.homeip.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii; format=flowed
X-Trace: news.uni-berlin.de 4UJl0rqAkU5BDkRvS3W4EwP7ZnEhll5lIWbPGh/nK2YA==
Return-Path: <cameron@cskk.id.au>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.017
X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; '(which': 0.04; 'def':
0.04; 'class,': 0.05; 'parameter': 0.05; 'class.': 0.07; 'alone.':
0.09; 'construct': 0.09; 'int': 0.09; 'namespace': 0.09;
'cheers,': 0.11; '>>>>': 0.16; 'cameron': 0.16; 'from:addr:cs':
0.16; 'from:addr:cskk.id.au': 0.16; 'from:name:cameron simpson':
0.16; 'instance': 0.16; 'lambda': 0.16; 'message-
id:@cskk.homeip.net': 0.16; 'namespace,': 0.16; 'received:10.10':
0.16; 'simpson': 0.16; 'wrote:': 0.16; 'name.': 0.19;
'subject:Question': 0.19; 'to:addr:python-list': 0.20; 'input':
0.21; 'i.e.': 0.22; 'returns': 0.22; 'code': 0.23;
'subject:using': 0.23; 'run': 0.23; 'depends': 0.25; 'saying':
0.25; 'normally': 0.26; 'function': 0.27; 'expect': 0.28;
'output': 0.28; 'header:User-Agent:1': 0.30; "doesn't": 0.32;
'encountered': 0.32; 'unexpected': 0.32; 'but': 0.32; 'header:In-
Reply-To:1': 0.34; 'using': 0.37; 'class': 0.37; 'means': 0.38;
"we've": 0.39; 'finding': 0.39; 'define': 0.40; 'method': 0.61;
"there's": 0.61; 'here': 0.62; 'leading': 0.63; 'pass': 0.64;
'your': 0.64; 'received:userid': 0.66; 'body': 0.67;
'header:Received:6': 0.67; 'during': 0.69; 'refers': 0.69;
'subject:method': 0.69; 'below': 0.69; 'process,': 0.75;
'received:172.16': 0.76; 'see,': 0.76; 'seek': 0.81; 'left': 0.83;
'happens': 0.84; 'attribute': 0.84; 'contrast,': 0.84;
'exceptions': 0.84; 'method,': 0.84; 'subject:behavior': 0.84;
'type.': 0.84; 'turned': 0.95
X-RG-Spam: Unknown
X-RazorGate-Vade: gggruggvucftvghtrhhoucdtuddrgedvhedrfedtfedgjeegucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuuffpveftpgfvgffnuffvtfetpdfqfgfvnecuuegrihhlohhuthemucegtddtnecunecujfgurhepfffhvffukfggtggujggffhesthdtredttdervdenucfhrhhomhepvegrmhgvrhhonhcuufhimhhpshhonhcuoegtshestghskhhkrdhiugdrrghuqeenucggtffrrghtthgvrhhnpeelveefgefhgefgheduvdfghefhffejgedvueeihfegleegkeefffeigfdutdevheenucfkphepuddruddvledruddvkedrvdegudenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhephhgvlhhopegsohhrghdrtghskhhkrdhhohhmvghiphdrnhgvthdpihhnvghtpedurdduvdelrdduvdekrddvgedupdhmrghilhhfrhhomheptggrmhgvrhhonhestghskhhkrdhiugdrrghupdhnsggprhgtphhtthhopedupdhrtghpthhtohepphihthhhohhnqdhlihhsthesphihthhhohhnrdhorhhgpdgruhhthhgpuhhsvghrpegtshhkkhessghighhpohhnugdrtghomhdpghgvohfkrfeptegfpdfovfetjfhoshhtpehnshhsthhlrhhguddvphdqshhvtg
X-RazorGate-Vade-Verdict: clean 0
X-RazorGate-Vade-Classification: clean
X-RG-VS-CLASS: clean
X-Authentication-Info: Submitted using ID cskk@bigpond.com
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
In-Reply-To: <CAEGro7UrWuNKwBV8HuW8geMk+NM4T=4_DRfztT1p0Md1EcTW3A@mail.gmail.com>
User-Agent: Mutt/2.2.7 (2022-08-07)
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <ZEHYCjlZyl0Eeelq@cskk.homeip.net>
X-Mailman-Original-References: <CAEGro7UrWuNKwBV8HuW8geMk+NM4T=4_DRfztT1p0Md1EcTW3A@mail.gmail.com>
 by: Cameron Simpson - Fri, 21 Apr 2023 00:25 UTC

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:
>
>```
>>>> class X:
>... __enter__ = int
>... __exit__ = lambda *_: None
>...
>>>> with X() as x:
>... pass
>...
>>>> x
>0
>```
>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.

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>


devel / comp.lang.python / Re: Question regarding unexpected behavior in using __enter__ method

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor