Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"I prefer rogues to imbeciles, because they sometimes take a rest." -- Alexandre Dumas (fils)


devel / comp.lang.python / Re: Superclass static method name from subclass

SubjectAuthor
o Re: Superclass static method name from subclassCameron Simpson

1
Re: Superclass static method name from subclass

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!fu-berlin.de!uni-berlin.de!not-for-mail
From: cs...@cskk.id.au (Cameron Simpson)
Newsgroups: comp.lang.python
Subject: Re: Superclass static method name from subclass
Date: Sat, 12 Nov 2022 09:17:26 +1100
Lines: 49
Message-ID: <mailman.929.1668205058.20444.python-list@python.org>
References: <ddbbb03e-4c0f-caa4-045f-4e745785ae3f@gmail.com>
<Y27J9pnOpo7lSGq9@cskk.homeip.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii; format=flowed
X-Trace: news.uni-berlin.de hpCA5dq0aIIoI9kw4wcnmA457C6XnKcjy3jzaWd56Aqg==
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.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip:@ 10': 0.03;
'(which': 0.04; 'def': 0.04; 'class.': 0.07; 'directly.': 0.07;
'explicitly': 0.07; 'subject:name': 0.07; 'anyway,': 0.09;
'example:': 0.09; 'methods,': 0.09; 'namespace': 0.09; 'skip:`
10': 0.09; 'cheers,': 0.11; 'cameron': 0.16; 'from:addr:cs': 0.16;
'from:addr:cskk.id.au': 0.16; 'from:name:cameron simpson': 0.16;
'instance': 0.16; 'message-id:@cskk.homeip.net': 0.16;
'received:13.237': 0.16; 'received:13.237.201': 0.16;
'received:13.237.201.189': 0.16; 'received:cskk.id.au': 0.16;
'received:id.au': 0.16; 'received:mail.cskk.id.au': 0.16;
'simpson': 0.16; 'static': 0.16; 'wrote:': 0.16; "can't": 0.17;
'to:addr:python-list': 0.20; 'maybe': 0.22; 'code': 0.23; 'run':
0.23; 'it,': 0.29; 'header:User-Agent:1': 0.30; 'think': 0.32;
'there': 0.33; 'header:In-Reply-To:1': 0.34; 'running': 0.34;
'bar': 0.35; 'received:au': 0.35; 'cases': 0.36; 'possibly': 0.36;
'posts': 0.36; "skip:' 10": 0.37; 'subject:from': 0.37; 'really':
0.37; 'class': 0.37; 'way': 0.38; 'valid': 0.39; 'use': 0.39;
'still': 0.40; 'define': 0.40; 'something': 0.40; 'want': 0.40;
'pass': 0.64; 'definition': 0.64; 'imagine': 0.64; 'received:13':
0.64; 'let': 0.66; 'received:userid': 0.66; 'time.': 0.66;
'generally': 0.67; 'during': 0.69; 'subject:method': 0.69;
'attribute': 0.84; 'method,': 0.84; 'standalone': 0.84
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
In-Reply-To: <ddbbb03e-4c0f-caa4-045f-4e745785ae3f@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: <Y27J9pnOpo7lSGq9@cskk.homeip.net>
X-Mailman-Original-References: <ddbbb03e-4c0f-caa4-045f-4e745785ae3f@gmail.com>
 by: Cameron Simpson - Fri, 11 Nov 2022 22:17 UTC

On 11Nov2022 10:21, Ian Pilcher <arequipeno@gmail.com> wrote:
>Is it possible to access the name of a superclass static method, when
>defining a subclass attribute, without specifically naming the super-
>class?
>
>Contrived example:
>
> class SuperClass(object):
> @staticmethod
> def foo():
> pass
>
> class SubClass(SuperClass):
> bar = SuperClass.foo
> ^^^^^^^^^^
>
>Is there a way to do this without specifically naming 'SuperClass'?

I think not.

All the posts so far run from inside methods, which execute after you've
got an instance of `SubClass`.

However, during the class definition the code under `class SubClass` is
running in a standalone namespace - not even inside a completed class.
When that code finished, that namespace is used to create the class
definition.

So you have no access to the `SuperClass` part of `class
SubClass(SuperClass):` in the class definition execution.

Generally it is better to name where something like this comes from
anyway, most of the time.

However, if you really want to plain "inherit" the class attribute
(which I can imagine valid use cases for), maybe write a property?

@property
def bar(self):
return super().foo

That would still require an instance to make use of it, so you can't
write explicitly `SubClass.bar`.

Possibly a metaclass would let you write a "class property", or to
define `bar` as a class attribute directly.

Cheers,
Cameron Simpson <cs@cskk.id.au>

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor