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


Groups > comp.lang.python > #42578

Re: extending class static members and inheritance

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <arnodel@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.008
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'derived': 0.09; 'subject:members': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'anyway': 0.14; 'question.': 0.14; '@classmethod': 0.16; '[])': 0.16; 'merged': 0.16; 'subject:class': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'pfxlen:0': 0.19; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'this?': 0.23; 'cc:2**0': 0.24; 'pass': 0.26; 'header:In- Reply-To:1': 0.27; 'received:209.85.217': 0.29; 'message- id:@mail.gmail.com': 0.30; 'way?': 0.31; 'class': 0.32; 'says': 0.33; 'could': 0.34; 'received:209.85': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'two': 0.37; 'received:209': 0.37; 'follows:': 0.38; 'extended': 0.61; '2013': 0.98; 'to:addr:hotmail.com': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=LDHvbvnICGyj0blV4jbUgom32/gJuvNXmQ3KgmujpLc=; b=a1UxQrRBrRhiYyEqEJNi0p+tcFXjZb7JW2egel2Qc3OObAGAnRbNYFC4H5nGcu+5UF 9y43152+ABvxA6cHc07hJlxZcREngrIt9HPPpWr4+rZpDzg2hPIUP2BsZ6d2hqXPY2Px eIGnnIKLZDbPrr8qYKPcydXDUcQgLTtFj0SPMxn47a0Kv/NRMJIyC8E81QHOcjx/cVxI y9DUcGU3Arz5mJ5qFdMRhuB/eQ952j/qzq+YKbUkX9rF/aG2ycZ2aJYHghYdbzczFQJj 9xoQXJICXyQSBqIReRBxwjqhkQDVO05cSPRBF2CL7BPvC9A6KPROm08s6Humq77ycl0N XxAg==
MIME-Version 1.0
X-Received by 10.112.25.202 with SMTP id e10mr8050267lbg.98.1364916282785; Tue, 02 Apr 2013 08:24:42 -0700 (PDT)
In-Reply-To <kjembp$ok3$1@ger.gmane.org>
References <kjembp$ok3$1@ger.gmane.org>
Date Tue, 2 Apr 2013 16:24:42 +0100
Subject Re: extending class static members and inheritance
From Arnaud Delobelle <arnodel@gmail.com>
To Fabian PyDEV <pydev@hotmail.com>
Content-Type text/plain; charset=UTF-8
Cc Python <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.8.1364916291.3114.python-list@python.org> (permalink)
Lines 67
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1364916291 news.xs4all.nl 6928 [2001:888:2000:d::a6]:53791
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:42578

Show key headers only | View raw


On 2 April 2013 14:27, Fabian PyDEV <pydev@hotmail.com> wrote:
> Hi All,
>
> I have a question.
>
> Let says I have the following two classes:
>
> class Base(object):
>         __mylist__ = ["value1", "value2"]
>
>         def somemethod(self):
>                 pass
>
>
> class Derived(Base):
>         __mylist__ = ["value3", "value4"]
>
>         def anothermethod(self):
>                 pass
>
>
>
>
> what I would like to accomplish is that the class Derived has the member __mylist__ extended or merged as ["value1", "value2", "value3", "value4"].
>
> Is there anyway I could accomplish this?
>
> I was thinking on accomplishing this as follows:
>
>
> class Derived(Base):
>         __mylist__ = Base.__mylist__ + ["value3", "value4"]
>
>         def anothermethod(self):
>                 pass
>
>
> Is there a better way? Perhaps a decorator?

class Base(object):

	mybits = ["value1", "value2"]

	@classmethod
	def mylist(cls):
		return sum((getattr(p, 'mybits', []) for p in cls.mro()[::-1]), [])


class Derived(Base):

	mybits = ["value3", "value4"]


class FurtherDerived(Derived):

	mybits = ["value5"]


>>> Derived.mylist()
['value1', 'value2', 'value3', 'value4']
>>> FurtherDerived.mylist()
['value1', 'value2', 'value3', 'value4', 'value5']

HTH

-- 
Arnaud

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


Thread

Re: extending class static members and inheritance Arnaud Delobelle <arnodel@gmail.com> - 2013-04-02 16:24 +0100

csiph-web