[Tutor] Multiples python files

Christopher Brookes chris.klaitos at gmail.com
Tue Mar 1 09:49:56 CET 2011


Thank you all for answers. Again.

2011/2/28 Alan Gauld <alan.gauld at btinternet.com>

>
> "Christopher Brookes" <chris.klaitos at gmail.com> wrote
>
>
>  I don't understand
>>
>> @classmethod
>>   def DisplayAll(cls, herosAll):
>>
>> What is cls ?
>>
>
> This is one of the advanced techniques I referred to a few days ago.
>
> Basically the "best"(?) solution to your problem is to store the list of
> characters inside the Character class as what is known as a class variable.
>
> Then everytime you create a new Character you can  add it to the list by
> the init method. And when a character is deleted you remove it via the del
> method.
>
> You can then define class methods to read/print the list, find out how many
> characters exist etc.
>
> This is much cleaner than keeping a separate global variable since the code
> for managing characters is all in one place with the classs definition. But
> it does introduce a bunch of new syntax features which I didn't think you
> were ready for yet! :-)
>
> One thing you will need to be very careful about as you go forward is
> namespaces. Remember that everytime you import a module you need to precede
> any names in that module with the module name. And names inside a class need
> to be preceded by the class name. And names inside objects need to be
> preceded by the object (variable) name.
> If the class is inside a module you need to use both module and class. Thus
> if the Character class is defined inside character.py you would have
> something like this in main.py:
>
> import character
> myObj = character.Character(....)
> print myObj.display()
> print character.Character.displayAll()
>
> etc.
>
> You can simplify it by using the "from m import v" style:
>
> from character import Character
> myObj = Character(....)
> myObj.display()
> Character.displayAll()
>
> etc.
>
> HTH,
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Brookes Christopher.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110301/2476bb51/attachment.html>


More information about the Tutor mailing list