Python Forum
I need help understanding a program structure using classes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I need help understanding a program structure using classes
#14
(Dec-26-2021, 08:40 PM)BashBedlam Wrote: Please run this and see if it helps you understand whyhandandself.handare 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)


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...)
Reply


Messages In This Thread
RE: I need help understanding a program structure using classes - by CompleteNewb - Dec-26-2021, 11:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Understanding Python classes PythonNewbee 3 1,252 Nov-10-2022, 11:07 PM
Last Post: deanhystad
  Understanding Python super() for classes OmegaRed94 1 1,884 Jun-09-2021, 09:02 AM
Last Post: buran
  Understanding program blocks newbieAuggie2019 2 2,060 Oct-02-2019, 06:22 PM
Last Post: newbieAuggie2019
  help with understanding a program prompt drasil 5 3,043 Feb-14-2019, 05:54 PM
Last Post: ichabod801
  Help, not understanding how classes work... Peter_EU 1 2,403 Jan-20-2018, 06:07 PM
Last Post: wavic
  Using classes? Can I just use classes to structure code? muteboy 5 5,159 Nov-01-2017, 04:20 PM
Last Post: metulburr
  I need help understanding how to use and run this program! Thanks in advance! tc1chosen 6 4,870 Sep-01-2017, 01:56 PM
Last Post: tc1chosen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020