I need help understanding a program structure using classes - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: I need help understanding a program structure using classes (/thread-35860.html) |
RE: I need help understanding a program structure using classes - BashBedlam - Dec-25-2021 Yes you can put them in a module and call them in your class file. If you use: import checkhandfunctionsThen you'll have to call the functions like this: checkhandfunctions.checkforpair (hand)If you use: import checkhandfunctions as chfThen you'll have to call the functions like this: chf.checkforpair (hand)And if you use: from checkhandfunctions import *Then you'll have to call the functions like this: checkforpair (hand)Got it? RE: I need help understanding a program structure using classes - CompleteNewb - Dec-26-2021 Yes, i got it. Thank you very much for your time. It was greatly appreciated. RE: I need help understanding a program structure using classes - BashBedlam - Dec-26-2021 Please run this and see if it helps you understand why hand andself.hand are two different things.class Player: flop = [] turn = [] river = ['This is Player.river'] def __init__(self, number, chips): self.number = number self.chips = chips self.hand = ['This is self.hand'] self.show_how_to_use_class_and_local_variables () def show_how_to_use_class_and_local_variables (self) : print ('\n') print (self.hand) print (Player.river) print ('Next is the error because "hand" is undefined.\n\n') print (hand) player1 = Player (7, 11) RE: I need help understanding a program structure using classes - CompleteNewb - Dec-26-2021 (Dec-26-2021, 08:40 PM)BashBedlam Wrote: Please run this and see if it helps you understand why Okay but jsut a couple more things: hand = 3#(1)<---------------I can have a global variable with the same name has an instance variable deck = [['1','H'], ['2','H'], ['3','H'], ['4','H'], ['5','H']] def pickacard(deck): card = deck[0] deck.remove(deck[0]) return card class Player: flop = [] turn = [] river = ['This is Player.river'] def __init__(self, number, chips): self.number = number #(2)<---------Since it's number I need to pass an argument when I create an object of that class chips = 4#(3)<----------------Since there's no self. before this doesn't work self.hand = []#(4)<------------Since i declare it has an empty list, I don't need to give an argument when i create an object, but how do append something do this list? self.show_how_to_use_class_and_local_variables () def show_how_to_use_class_and_local_variables (self) : print ('\n') print (self.hand) print (Player.river) print ('Next is the error because "hand" is undefined.\n\n') print (hand)#<----------------This calls the global variable, not the instance variable #print(self.chips)#(5)Doesn't work because you can't declare a instance variable like this def pickplayershand(self, deck, pickacard): #<--------- Here (6) card = pickacard(deck) deck.remove(deck[0]) hand.append(card)#<--------------------And here(7) card = pickacard(deck) deck.remove(deck[0]) hand.append(card) return player1 = Player (7, 11) print(player1.hand)How can I modifie self.hand? (4) #(6) How do i pass the instance variable hand as an argument? self.hand or Player.hand? #(7)How do I append something to an instance variable list ? Do I write self.hand.append(card)???? (it doesn't work...) RE: I need help understanding a program structure using classes - BashBedlam - Dec-27-2021 # 1 Yes you can have a global variable by the same name but I don't recommend it. # 2 Yes because number is an argument and self.number is a variable that is set to that value. # 3 chips will work correctly inside of "__init__" but nowhere else. # 4 I recommend a setter method inside of your class that contains something like "self.hand.append (argument)". # 5 Correct. # 6 Will work but would be better if "picacard" was a method inside of your Player class and called like this "self.picacard (deck). # 7 Will not work. In order to modify a global variable you would have to declare it as global inside of your class hand = 3 #(1)<---------------I can have a global variable with the same name has an instance variable deck = [['1','H'], ['2','H'], ['3','H'], ['4','H'], ['5','H']] def pickacard(deck): card = deck[0] deck.remove(deck[0]) return card class Player: flop = [] turn = [] river = ['This is Player.river'] def __init__(self, number, chips): self.number = number #(2)<---------Since it's number I need to pass an argument when I create an object of that class chips = 4#(3)<----------------Since there's no self. before this doesn't work self.hand = []#(4)<------------Since i declare it has an empty list, I don't need to give an argument when i create an object, but how do append something do this list? self.show_how_to_use_class_and_local_variables () def show_how_to_use_class_and_local_variables (self) : print ('\n') print (self.hand) print (Player.river) print ('Next is the error because "hand" is undefined.\n\n') print (hand)#<----------------This calls the global variable, not the instance variable #print(self.chips)#(5)Doesn't work because you can't declare a instance variable like this def pickplayershand(self, deck, pickacard): #<--------- Here (6) card = pickacard(deck) deck.remove(deck[0]) hand.append(card)#<--------------------And here(7) card = pickacard(deck) deck.remove(deck[0]) hand.append(card) return player1 = Player (7, 11) print(player1.hand) RE: I need help understanding a program structure using classes - CompleteNewb - Dec-27-2021 So it would be impossible to change self.hand in a function inside the class without a setter? class Player: flop = [] turn = [] river = ['This is Player.river'] def __init__(self, number, chips): self.number = number self.chips = chips self.hand = [] def pickplayershandwithclassvariable(self, deck): card = pickacard(deck) variable.append(card) #Change self.hand here by adding card to the self.hand list return self.hand RE: I need help understanding a program structure using classes - BashBedlam - Dec-27-2021 (Dec-27-2021, 02:52 AM)CompleteNewb Wrote: So it would be impossible to change self.hand in a function inside the class without a setter?No. It's just better to do it that way. All of your instance variables CAN be accessed from outside of the instance, it's just not the best programming practice to do so. class Player: def __init__(self, number, chips): self.number = number self.chips = chips self.hand = [] player1 = Player (1, 2) player1.hand = [player1.number, player1.chips] print (player1.hand) RE: I need help understanding a program structure using classes - deanhystad - Dec-27-2021 I strongly disagree that setters and getters are better than directly accessing attributes. Use a setter when setting an attribute requires additional processing. For example, if your objects are entries in a database you probably want to modify the database when you set the value of an attribute. This is easy to do with a setter method and/or using the property decorator. However, if your attribute is just a variable and setting the variable doesn't require any additional processing, I would not use a setter. The thinking that setters and getters make better code can be traced back to poorly designed languages like C++ and all their derivatives and the languages they influenced. Here you often needed setters and getters, or using setters and getters provides some level of protection by limiting how you could access the attributes. In Python a setter/getter provides no projection at all and usually just adds extra code that has to be maintained. Never, ever use "from module import *". The reason for modules is primarily to generate name spaces, not reduce the amount of code in a file. When you create a namespace you are protection your variable and class names from colliding with variables or class names in code written by other people. Tkinter, a popular Python GUI package, has classes with names like Label and Button. These are nice names. If I make my own type of button I would probably like to call it Button. Since the tkinter Button is tkinter.Button I can do that and not worry about my program confusing my buttons with the tkinter buttons. When you "from module inport *" you strip the namespace protection and all the imported attributes. RE: I need help understanding a program structure using classes - BashBedlam - Dec-27-2021 I think you should look again at this post by deanhystad. It is an excellent example of how python and OOP work. (Dec-24-2021, 01:32 PM)deanhystad Wrote: I think you should make a Card class and a Hand class. The Card class knows how to do things like print its name and sort itself among a list of cards. The Hand class would know how to identify pairs and flushes. Mine is incomplete but should give some idea of how this could be done. The Player would be delt a hand, place bets and acquire or pay out chips. RE: I need help understanding a program structure using classes - CompleteNewb - Dec-28-2021 It is, but it's also a bit of a cognitive overload... Anyway, I have to thank you because I have learned a lot. I also have a lot more question, but they're getting less related to the original post(even if we strayed a little) I am very grateful for your help. Thank you |