Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #42562 > unrolled thread
| Started by | Dave Angel <davea@davea.name> |
|---|---|
| First post | 2013-04-02 09:42 -0400 |
| Last post | 2013-04-02 09:42 -0400 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: extending class static members and inheritance Dave Angel <davea@davea.name> - 2013-04-02 09:42 -0400
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-04-02 09:42 -0400 |
| Subject | Re: extending class static members and inheritance |
| Message-ID | <mailman.39.1364910192.17481.python-list@python.org> |
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 top | Article view | comp.lang.python
csiph-web