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


Groups > comp.lang.python > #42562

Re: extending class static members and inheritance

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'attributes': 0.09; 'derived': 0.09; 'subject:members': 0.09; 'def': 0.12; 'anyway': 0.14; 'language.': 0.14; 'question.': 0.14; 'attribute,': 0.16; 'attributes.': 0.16; 'entirely.': 0.16; 'merged': 0.16; 'subject:class': 0.16; 'underscore.': 0.16; 'underscores': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'this?': 0.23; 'header:User- Agent:1': 0.23; 'of.': 0.24; 'pass': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'leave': 0.29; 'am,': 0.29; 'way?': 0.31; 'class': 0.32; 'says': 0.33; "i'd": 0.34; 'could': 0.34; 'but': 0.35; 'there': 0.35; 'done': 0.36; 'should': 0.36; 'two': 0.37; 'follows:': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'called': 0.40; 'extended': 0.61; 'fulfill': 0.68; 'received:74.208': 0.68; 'special': 0.74; 'received:74.208.4.194': 0.84
Date Tue, 02 Apr 2013 09:42:44 -0400
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130308 Thunderbird/17.0.4
MIME-Version 1.0
To python-list@python.org
Subject Re: extending class static members and inheritance
References <kjembp$ok3$1@ger.gmane.org>
In-Reply-To <kjembp$ok3$1@ger.gmane.org>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:bizJpL8qWq520e/RbgCxrzTy9n99e5IKWGgD18Z/rV7 AF6hC0ydshdBJgDj1g87xVYSKsn0xLsXdW451YQJq8kiAhmX6d KgGA/O9Zm+C1xOm+OtsXRg0Kejb23ULolLFcGG871vZmis189b c9tgX7Y3ZPTU7yr5zCJNts3NxT02ko89B2iy/aXXWP8y+QPnHD v5QiiAPtolg13CRuQ8gZtBjI/l8D73WC62fDQ0P1JGrrhokp3j e4YGdX9alr55e9vDl9XRFt+nhE5wkYEo8kLoZCV6Bc1jtoCXUA SkHBhgbz9iKb4CkVvE5LGFBI6gE3drbWJsCGUShQyJ9FfneWg= =
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.39.1364910192.17481.python-list@python.org> (permalink)
Lines 54
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1364910192 news.xs4all.nl 6975 [2001:888:2000:d::a6]:53149
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:42562

Show key headers only | View raw


On 04/02/2013 09:27 AM, Fabian PyDEV 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?
>

This is already done the best (clearest) way I know of.

However, I'd like to point out two things:
1) they're not called class members, but class attributes.  You have 
class attributes and instance attributes.

2) dunder methods should only be used to fulfill special methods defined 
by the language.  If it's a public attribute, just leave off the 
underscores entirely. And if it's private, put just one leading underscore.



-- 
DaveA

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


Thread

Re: extending class static members and inheritance Dave Angel <davea@davea.name> - 2013-04-02 09:42 -0400

csiph-web