optional argument to a subclass of a class

Chris Rebert clp2 at rebertia.com
Fri May 21 15:01:43 EDT 2010


On Fri, May 21, 2010 at 11:49 AM, Chris Rebert <clp2 at rebertia.com> wrote:
> On Fri, May 21, 2010 at 11:38 AM, Alex Hall <mehgcap at gmail.com> wrote:
>> On 5/21/10, Christian Heimes <lists at cheimes.de> wrote:
>>> Am 21.05.2010 04:56, schrieb Alex Hall:
>>>> Hi all,
>>>> I am now trying to allow my classes, all of which subclass a single
>>>> class (if that is the term), to provide optional arguments. Here is
>>>> some of my code:
>>>>
>>>> class Craft():
>>>>   def __init__(self,
>>>>   name,
>>>>   isAircraft=False,
>>>>   id=helpers.id(),
>>>>   hits=0,
>>>>   weapons=[]):
>>>
>>> I hope you are aware that helpers.id() is called just once when the
>>> *class* is defined and that the list weapons is shared across all
>>> instances of the craft class. :)
>> I know id is called once, but what do you mean weapons is shared?
>
> Read the "Important warning" on
> http://docs.python.org/tutorial/controlflow.html#default-argument-values
> `weapons` will be the *exact same list object* for all Craft instances
> the way you have it currently (i.e. it will not be copied and changes
> to it will affect all Crafts).

Er, all Craft instances /where you didn't pass an explicit `weapons`
argument to the constructor/, that is.

- Chris



More information about the Python-list mailing list