Re: [Tutor] Multiples python files
Thank you all for answers. Again. 2011/2/28 Alan Gauld > > "Christopher Brookes" 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@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Brookes Christopher. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Multiples python files
I don't understand @classmethod def DisplayAll(cls, herosAll): What is cls ? 2011/2/28 Izz ad-Din Ruhulessin > @classmethod > def DisplayAll(herosAll): > > is of course: > > @classmethod > def DisplayAll(cls, herosAll): > > 2011/2/28 Izz ad-Din Ruhulessin > > Hi, you must pass the herosAll list to the DisplayAll() method like this >> >> class Character(): >> @classmethod >> def DisplayAll(herosAll): >> print ('There is', Character.CharacterCount, 'heros') >> for heros in herosAll: >> heros.DisplayCharacterInfos() >> >> herosAll = [ >> Character(1,"Antaa","Soldat moins fort",15,5,8), >> Character(2,"Klaitos","Soldat moins fort",15,5,8)] >> >> Character.DisplayAll(herosAll) >> >> >> 2011/2/28 Christopher Brookes >> >>> Hi, first sorry for my poor english, i'm a french guy, i'm trying to >>> make the best :( >>> >>> I would like to split my python script into multiples files. >>> I want : >>> A file which contains only class creations and methods, >>> A file with some personals functions >>> And a main.py which is the main script. >>> >>> But i'm getting some problems >>> In class creation file, i've a init method which create a character (it >>> works). >>> They are created in main.py like this : >>> >>> herosAll = [ >>> Character(1,"Antaa","Soldat moins fort",15,5,8), >>> Character(2,"Klaitos","Soldat moins fort",15,5,8)] >>> >>> But when i want to display all information about my character with : >>> >>> class Character(): >>> def DisplayAll(): >>> print ('There is', Character.CharacterCount, 'heros') >>> for heros in herosAll: >>> heros.DisplayCharacterInfos() >>> >>> I'm getting : >>> >>> Traceback (most recent call last): >>> File "main.py", line 28, in >>> Character.DisplayAll() >>> File "/home/christopher/class_creation.py", line 53, in DisplayAll >>> for heros in herosAll: >>> NameError: global name 'herosAll' is not defined >>> >>> I know the source of the problem. The list herosAll is declared in >>> main.py so he can't access to it. But i'm stuck here. How can he get it ? >>> Have I to append the list in the init method ? >>> >>> Thank you for ready. >>> >>> >>> >>> -- >>> Brookes Christopher. >>> >>> ___ >>> Tutor maillist - Tutor@python.org >>> To unsubscribe or change subscription options: >>> http://mail.python.org/mailman/listinfo/tutor >>> >>> >> > -- Brookes Christopher. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Multiples python files
Hi, first sorry for my poor english, i'm a french guy, i'm trying to make the best :( I would like to split my python script into multiples files. I want : A file which contains only class creations and methods, A file with some personals functions And a main.py which is the main script. But i'm getting some problems In class creation file, i've a init method which create a character (it works). They are created in main.py like this : herosAll = [ Character(1,"Antaa","Soldat moins fort",15,5,8), Character(2,"Klaitos","Soldat moins fort",15,5,8)] But when i want to display all information about my character with : class Character(): def DisplayAll(): print ('There is', Character.CharacterCount, 'heros') for heros in herosAll: heros.DisplayCharacterInfos() I'm getting : Traceback (most recent call last): File "main.py", line 28, in Character.DisplayAll() File "/home/christopher/class_creation.py", line 53, in DisplayAll for heros in herosAll: NameError: global name 'herosAll' is not defined I know the source of the problem. The list herosAll is declared in main.py so he can't access to it. But i'm stuck here. How can he get it ? Have I to append the list in the init method ? Thank you for ready. -- Brookes Christopher. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Object, methods, class
Hi, Is there in Python private/protected attributes in class like in other langage ? -- Brookes Christopher. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Search function in a list-tuples
Hi, I found the solution :) Special thanks to Alan G. Solution : def FindByName(HerosName): for heros in herosAll: if HerosName == heros.name: return heros return None HerosName = 'notfound' while FindByName(HerosName) == None: HerosName=input("Enter heros name (type exit for close): ") if HerosName.lower() == 'exit': Display_menu() else: ch = FindByName(HerosName) if ch: ch.DisplayCharacterInfos() newSearch=input('New search ? (Y/N)') else : print ('This heros does\'nt exist') 2011/2/25 ALAN GAULD > CCing group. Please use ReplyAll in replies to the list. > > > > Hi, thank your for your answer. i wrote this now : > > > def HeroExist(HerosName): > for heros in herosAll: > if HerosName in heros.name: > herosId = heros.id > return herosId > return None > > HerosName=input("Enter heros name : ") > ch = HeroExist(HerosName) > if ch: > ch.DisplayCharacterInfos() > else : > print ('This heros does\'nt exist') > > But whatever i type, he says : heros doesn't exist :/ > > That suggets a problem with how the name is being stored. > Can you send the code for the Character class, esp[ecially the init method. > Plus a cut n paste of the output of a ruin of the programme so that > we can see exactly what you are inputting and what the > results are? > > BTW you don't need the \ in the last print. You are using double quotes > around the string so you can just type the single quote directly > > print ( "This hero doesn't exist') > > Alan G. > > 2011/2/25 Alan Gauld > >> "Christopher Brookes" wrote >> >> Hi, is there a better way to do this ? (*heros are Character*) >>> >> >> There are several better ways but mainly they involve >> techniques that may be too advanced for you at present, >> so we'll keep it simple for now :-) >> >> >> herosAll = [ >>> Character(0,"Chris","Soldat fort",type[0],15,5,8,50,1), >>> Character(1,"Antaa","Soldat moins fort",type[0],15,5,8,50,1)] >>> >>> def HeroExist(HerosName): >>> herosId = -1 >>> for heros in herosAll: >>> if HerosName in heros.name: >>> herosId = heros.id >>> if herosId != -1: >>> return herosId >>> else: >>> return -1 >>> >> >> replace from the for loop down with: >> >> >> for heros in herosAll: >> if HerosName in heros.name: >> herosId = heros.id >> return herosId >> return None # This allows more convenient testing, see below >> >> Which has the advantage of being slightly faster too. >> >> >>HerosName=input("Enter heros name : ") >>> if Character.HeroExist(HerosName) != -1: >>> >> >> HeroExist is not a method of Character ( although you could >> make it so - one of the better solutions I alluded to above! ) >> So you don't need to prefix it with Character. >> >> And you can assign the return value to a variable so >> you avoid calling it twice: >> >> >> HerosName=input("Enter heros name : ") >> ch = HeroExist(HerosName): >> if ch: # because None is treated as False >> ch.DisplayCharacterInfos() >> >> else : >> print ('This heros does\'nt exist') >> >> HTH, >> >> >> -- >> Alan Gauld >> Author of the Learn to Program web site >> http://www.alan-g.me.uk/ >> >> >> >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > > > > -- > Brookes Christopher. > > -- Brookes Christopher. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Search function in a list-tuples
Hi, is there a better way to do this ? (*heros are Character*) herosAll = [ Character(0,"Chris","Soldat fort",type[0],15,5,8,50,1), Character(1,"Antaa","Soldat moins fort",type[0],15,5,8,50,1)] def HeroExist(HerosName): herosId = -1 for heros in herosAll: if HerosName in heros.name: herosId = heros.id if herosId != -1: return herosId else: return -1 I find this solution myself and I think a better one exist.. HerosName=input("Enter heros name : ") if Character.HeroExist(HerosName) != -1: herosAll[Character.HeroExist(HerosName)].DisplayCharacterInfos() else : print ('This heros does\'nt exist') Display_menu() Ty :) -- Brookes Christopher. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Display all field of a listuples
It's works. Thank you all. 2011/2/25 bob gailer > On 2/24/2011 4:54 PM, Christopher Brookes wrote: > > Hi i would like to display all the field of my powerAll like this : > > Choose a power : > Froid devorant : Embrase lenemi et le feu bruler > Flammes infernales : 'Gele lenemi sur place > > - > class Character(): > def ChoosePouvoirUnique(self): > print ("Choose a power") > for Power in powerAll: > print (Power) > > class Power: > def __init__(self, name, desc): > self.name = name > self.desc = desc > > > > > powerAll = [ > Power('Flammes infernales' , 'Embrase lenemi et le feu bruler'), > Power('Froid devorant', 'Gele lenemi sur place')] > > > But he won't display it :( > > i've try For PowerName,PowerDesc in powerAll: > print (PowerName, PowerDesc) > > but it doesn't work ! > > > When something "does not work" always show us what results you got as well > as what you wanted. > > Also get in the habit of using Capitalized names for classes and > unCapitalized names for variables and functions. > > powerAll is a list of class instaces. You must (in some way) identify the > attributes of those instances. One way: > > for power in powerAll: > print (power.name, power.desc) > > -- > Bob Gailer > 919-636-4239 > Chapel Hill NC > > -- Brookes Christopher. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Display all field of a listuples
class Character: def __init__(self, name): self.name = name def ChoosePouvoirUnique(self): """ Permet de choisir le pouvoir unique du personnage """ print ("Veuillez choisir votre pouvoir unique dans la liste") for PowerNom,PowerDesc in powerAll: print (PowerNom, PowerDesc) class Power: def __init__(self, name, desc): self.name = name self.desc = desc powerAll = [ Power('Flammes infernales' , 'Embrase lenemi et le feu bruler'), Power('Froid devorant', 'Gele lenemi sur place')] hero1 = Character("Klaitos") hero1.ChoosePouvoirUnique() im *WANT to display this* : Froid devorant : Gele lenemi sur place Flammes infernales : Embrase lenemi et le feu bruler I don't know how to get this ? :( 2011/2/24 Steven D'Aprano > Alex Hall wrote: > >> On 2/24/11, Christopher Brookes wrote: >> >>> Hi i would like to display all the field of my powerAll like this : >>> >>> Choose a power : >>> Froid devorant : Embrase lenemi et le feu bruler >>> Flammes infernales : 'Gele lenemi sur place >>> >>> - >>> class Character(): >>> def ChoosePouvoirUnique(self): >>>print ("Choose a power") >>>for Power in powerAll: >>>print (Power) >>> >> > You need a __init__() function in this class, as with any class. >> > > That's not strictly correct. You don't *need* an __init__ method, unless > your class needs to be initialised. Most classes will, but some do not. > > > [...] > > Also, "for" should be lowercase, though I am >> honestly not sure that this is a requirement(though I believe it is). >> > > It certainly is. Python is case sensitive, so "for" and "FOR" and "For" are > different. "for" is a Python keyword with special meaning. The others are > just words with no special meaning. > > > > > -- > Steven > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Brookes Christopher. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Display all field of a listuples
Hi i would like to display all the field of my powerAll like this : Choose a power : Froid devorant : Embrase lenemi et le feu bruler Flammes infernales : 'Gele lenemi sur place - class Character(): def ChoosePouvoirUnique(self): print ("Choose a power") for Power in powerAll: print (Power) class Power: def __init__(self, name, desc): self.name = name self.desc = desc powerAll = [ Power('Flammes infernales' , 'Embrase lenemi et le feu bruler'), Power('Froid devorant', 'Gele lenemi sur place')] But he won't display it :( i've try For PowerName,PowerDesc in powerAll: print (PowerName, PowerDesc) but it doesn't work ! Thank you again.. -- Brookes Christopher. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Dictionnaries in object
Hi, I want to create some powers in my fight program. I want to know if dictionnaries is the best solution to do it. For now its look like this : //French name and description, don't care about it ;) power1= {} power1['Name'] = 'Flammes infernales' power1['Description'] = 'Embrase lenemi et le feu bruler' power2= {} power2['Name'] = 'Froid devorant' power2['Description'] = 'Gele lenemi sur place' powerAll= [power1,power2] but if i want to create like 20 powers, it will be long no ? is there any solution shorter (and better ?) Thank you for reading, -- Brookes Christopher. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python object
Hi, i'm new in python. I'm trying to create a small fight program in object. I've created __init__ (its works) but when i'm trying to display init param i'm getting param and "None" every time. Why ? def GetAllAtrib(self): print '---' print self.name print self.description print self.type print '---' give ->> --- Klaitos Soldier very strong Soldier --- *None *-- WHY ARE U HERE ?? Yours, -- Brookes Christopher. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor