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


Groups > comp.lang.python > #2342

Re: Alias for an attribute defined in a superclass

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <prvs=065e69a23=jeanmichel@sequans.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'attributes': 0.05; 'hettinger': 0.07; 'names.': 0.07; 'attribute': 0.09; 'attribute.': 0.09; 'attributes,': 0.09; 'override': 0.09; 'underlying': 0.09; 'pm,': 0.11; 'def': 0.13; 'wrote:': 0.14; '@property': 0.16; 'aliases': 0.16; 'attributes.': 0.16; 'finney': 0.16; 'methods,': 0.16; 'received:192.168.200': 0.16; 'class,': 0.16; 'wrap': 0.19; 'header:In-Reply-To:1': 0.22; 'raymond': 0.22; 'skip:_ 20': 0.24; 'define': 0.26; 'pass': 0.27; 'class': 0.29; 'fine.': 0.29; 'all,': 0.31; 'to:addr:python-list': 0.32; 'idea': 0.32; 'received:192': 0.34; 'header:User-Agent:1': 0.35; 'received:192.168': 0.37; 'it?': 0.37; 'skip:o 20': 0.37; 'refer': 0.37; 'but': 0.38; 'current': 0.38; 'set': 0.39; 'to:addr:python.org': 0.39; 'could': 0.39; 'how': 0.39; 'charset:windows-1252': 0.61; 'mar': 0.64; '\x93the': 0.91
X-IronPort-AV E=Sophos;i="4.63,281,1299452400"; d="scan'208";a="1174383"
X-Virus-Scanned amavisd-new at zimbra.sequans.com
Date Fri, 01 Apr 2011 11:54:06 +0200
From Jean-Michel Pichavant <jeanmichel@sequans.com>
User-Agent Mozilla-Thunderbird 2.0.0.24 (X11/20100328)
MIME-Version 1.0
To python-list@python.org
Subject Re: Alias for an attribute defined in a superclass
References <87sju3ndjo.fsf@benfinney.id.au> <787d36b9-e31d-4b61-a14e-770337cfc280@a11g2000pro.googlegroups.com>
In-Reply-To <787d36b9-e31d-4b61-a14e-770337cfc280@a11g2000pro.googlegroups.com>
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 8bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.68.1301651655.2990.python-list@python.org> (permalink)
Lines 64
NNTP-Posting-Host 82.94.164.166
X-Trace 1301651655 news.xs4all.nl 41110 [::ffff:82.94.164.166]:51509
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:2342

Show key headers only | View raw


Raymond Hettinger wrote:
> On Mar 31, 3:14 pm, Ben Finney <ben+pyt...@benfinney.id.au> wrote:
>   
>> Howdy all,
>>
>> I want to inherit from a class, and define aliases for many of its
>> attributes. How can I refer to “the attribute that will be available by
>> name ‘spam’ once this class is defined”?
>>
>>     class Foo(object):
>>         def spam(self):
>>             pass
>>
>>         def eggs(self):
>>             pass
>>
>>     class Bar(Foo):
>>         beans = Foo.spam
>>         mash = Foo.eggs
>>
>> Is that the right way to do it?
>>     
>
> For methods, that will work just fine.  For attributes, you will need
> to make @property accessors that get and set the underlying attribute.
>
>
> Raymond
>   
For attributes you could also override __getattribute__ & __setattr__ to 
wrap Bar's names into Foo's names.
Could work also for methods, but for methods your current idea is much 
more simple.

class Foo(object):
    def __init__(self):
       self.spam = 'someSpam'
       self.eggs = 'fewEggs'

    def vanilla(self):
        return self.spam

class Bar(Foo):
    _map = {
       'beans': 'spam',
       'mash': 'eggs',
       'chocolate': 'vanilla',
    }
    def __getattribute__(self, attribute):
        getSafely = object.__getattribute__
        _map =  getSafely(self, '_map')
        if attribute in _map:
            return getSafely(self, _map[attribute])
        else:
            return getSafely(self, attribute)


Bar().chocolate()
'someSpam'

Bar().mash
'fewEggs'

JM

Back to comp.lang.python | Previous | NextPrevious 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