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
#3
(Dec-24-2021, 02:02 AM)BashBedlam Wrote: We can't run your code becausestraightfunction,pairfunctionandflushfunctionare missing but I can help you understand why the variablehandis throwing the error that you're getting.
Variables defined in a class before the__init__method are called "class variables". They are called that because they are shared by every instance of that class. In the code below I show you how to set a class variable but more importantly, why you shouldn't try to use them the way that you are. The reason for this is becausePlayer.handis the same for bothplayer_oneandplayer_two.
class Player :
	hand = []
	def __init__ (self, number, chips) :
		self.number = number
		self.chips = chips

player_one = Player (7, 11)
player_two = Player (7, 13)

Player.hand = [['1', 'H'],['2', 'D'],['3', 'C'],['4', 'S']]

print (f'Hand for player one is {one.hand}.')
print (f'Hand for player two is {two.hand}.')

Thanks for answering back. I really appreciate it. So i put them in the constructor? But what about the flop? Do i put the flop, turn and river as class variables and change it at every turn, because I can't pass variable as a argument in a function called inside a function inside a class?
Reply


Messages In This Thread
RE: I need help understanding a program structure using classes - by CompleteNewb - Dec-24-2021, 05:05 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PLEASE DELETE_Python OOP program structure TeddyT 0 375 Apr-26-2025, 10:00 AM
Last Post: TeddyT
  Understanding Python classes PythonNewbee 3 2,279 Nov-10-2022, 11:07 PM
Last Post: deanhystad
  Understanding Python super() for classes OmegaRed94 1 2,517 Jun-09-2021, 09:02 AM
Last Post: buran
  Understanding program blocks newbieAuggie2019 2 2,858 Oct-02-2019, 06:22 PM
Last Post: newbieAuggie2019
  help with understanding a program prompt drasil 5 4,195 Feb-14-2019, 05:54 PM
Last Post: ichabod801
  Help, not understanding how classes work... Peter_EU 1 3,185 Jan-20-2018, 06:07 PM
Last Post: wavic
  Using classes? Can I just use classes to structure code? muteboy 5 6,554 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 6,491 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