Aug-10-2021, 06:55 PM
(using 3.9)
I tried to search for this subject but couldn't find the right wording (typical problem).
I have a function I am passing an object with attributes ('hand'). Hand has four attributes and I end up with dozens of references to those attributes by hand.this and hand.that.
What seems 'pythonic' would be a way to 'namespace' the argument for lack of a better word so I can just use 'this' and 'that'. For now I copied the four arguments into local variables, did all my work with those, then copied them back to the argument object. Here is my code with my klunky fix:
I tried to search for this subject but couldn't find the right wording (typical problem).
I have a function I am passing an object with attributes ('hand'). Hand has four attributes and I end up with dozens of references to those attributes by hand.this and hand.that.
What seems 'pythonic' would be a way to 'namespace' the argument for lack of a better word so I can just use 'this' and 'that'. For now I copied the four arguments into local variables, did all my work with those, then copied them back to the argument object. Here is my code with my klunky fix:
def updatevalue(hand): cards=hand.cards aceslow=hand.aceslow aceshigh=hand.aceshigh value=sum(cards)+(aceshigh*10) #for every aceshigh add 10 while (value > 21) and (aceshigh > 0): #lower aces until hand is <= 21 or no more aces aceshigh -= 1 value -= 10 aceslow += 1 print(f'Current hand: {value}, aces high: {aceshigh}, aces low: {aceslow}') while aceshigh !=0 and value < 21: #optional downgrades of remaining aces reply=input('Downgrade a remaining Ace?') if reply not in ('y','Y','n','N'): reply = 'y' print('Hunh? Try again, y or n.') continue elif reply.capitalize() =='Y': aceshigh -= 1 value -= 10 aceslow += 1 continue else: break print('---------------------------------') print(f'Final hand: {value}, aces high: {aceshigh}, aces low: {aceslow}') hand.value = value hand.cards = cards hand.aceslow = aceslow hand.aceshigh = aceshigh return value