Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #2325

Re: Alias for an attribute defined in a superclass

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!news-out.octanews.net!mauve.octanews.net!news.astraweb.com!border6.newsrouter.astraweb.com!not-for-mail
From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Re: Alias for an attribute defined in a superclass
References <87sju3ndjo.fsf@benfinney.id.au> <4d9518f1$0$29992$c3e8da3$5496439d@news.astraweb.com>
X-Public-Key-ID 0xAC128405
X-Public-Key-Fingerprint 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-pubkey.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
Date Fri, 01 Apr 2011 11:59:47 +1100
Message-ID <878vvuokfw.fsf@benfinney.id.au> (permalink)
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
Cancel-Lock sha1:GjTnjgb3CQfxMCkqj6eMuO5PFNs=
MIME-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
Lines 54
Organization Unlimited download news at news.astraweb.com
NNTP-Posting-Host 9ff75274.news.astraweb.com
X-Trace DXC=G2Roo@n\K<?YUQJRTa54B1L?0kYOcDh@:SBc;\8ijUd;QS[jEnCJZX1UGD@iN[Hd??dKF`JIMc=\7
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:2325

Show key headers only | View raw


Steven D'Aprano <steve+comp.lang.python@pearwood.info> writes:

> On Fri, 01 Apr 2011 09:14:03 +1100, Ben Finney wrote:
>
> > I want to inherit from a class, and define aliases for many of its
> > attributes.
>
> Are these aliases of arbitrary aliases, or only of methods, as in your
> example below?

I'd like to know how to do either, for making API wrapper classes.

> > How can I refer to “the attribute that will be available by
> > name ‘spam’ once this class is defined”?
>
> You might be able to use the descriptor protocol to do something like
> that, but otherwise I don't think you can. However, I note that your
> example isn't *quite* how you describe it above.

Yes, I got the names wrong. The example is accurate to what I'm meaning
to say.

> >     class Foo(object):
> >         def spam(self):
> >             pass
> >         def eggs(self):
> >             pass
> > 
> >     class Bar(Foo):
> >         beans = Foo.spam
> >         mash = Foo.eggs
>
> This assigns the name Bar.beans to the method object Foo.spam. If you
> now rebind the name Foo.spam to something else, Bar.beans will not
> likewise change, but will continue to refer to the original. This is
> contrary to your earlier description, where Bar.beans should also
> change.

Hmm. I think I can live with that limitation.

> > Is that the right way to do it? Will that leave me open to “unbound
> > method” or “is not an instance of ‘Bar’” or other problems when
> > using ‘Bar.beans’?
>
> I don't believe so. So long as you don't rebind the "alias" or the 
> original, you should be fine.

Okay, thank you.

-- 
 \      “I am too firm in my consciousness of the marvelous to be ever |
  `\       fascinated by the mere supernatural …” —Joseph Conrad, _The |
_o__)                                                     Shadow-Line_ |
Ben Finney

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Alias for an attribute defined in a superclass Ben Finney <ben+python@benfinney.id.au> - 2011-04-01 09:14 +1100
  Re: Alias for an attribute defined in a superclass Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-04-01 00:14 +0000
    Re: Alias for an attribute defined in a superclass Ben Finney <ben+python@benfinney.id.au> - 2011-04-01 11:59 +1100
  Re: Alias for an attribute defined in a superclass Calvin Spealman <ironfroggy@gmail.com> - 2011-03-31 20:36 -0400
    Re: Alias for an attribute defined in a superclass Ben Finney <ben+python@benfinney.id.au> - 2011-04-01 11:57 +1100
  Re: Alias for an attribute defined in a superclass Raymond Hettinger <python@rcn.com> - 2011-03-31 19:24 -0700
    Re: Alias for an attribute defined in a superclass Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-04-01 11:54 +0200

csiph-web