Python Forum
'namespace' shorthand for function arguments?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'namespace' shorthand for function arguments?
#1
(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:
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
Reply


Messages In This Thread
'namespace' shorthand for function arguments? - by shadowphile - Aug-10-2021, 06:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  calling external function with arguments Wimpy_Wellington 7 1,527 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  If with For statement shorthand? kaega2 5 1,147 Sep-06-2022, 08:12 PM
Last Post: Gribouillis
  Checking the number of arguments a function takes Chirumer 3 2,211 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Possible to dynamically pass arguments to a function? grimm1111 2 2,229 Feb-21-2021, 05:57 AM
Last Post: deanhystad
  [PyKML] Loop through all Placemarks; Remove namespace Winfried 2 3,474 Aug-28-2020, 09:24 AM
Last Post: Winfried
  How to pass multiple arguments into function Mekala 4 2,486 Jul-11-2020, 07:03 AM
Last Post: Mekala
  How to give a name to function arguments in C-API? WonszZeczny 0 1,371 Jun-22-2020, 10:20 AM
Last Post: WonszZeczny
  cant understand the logic behind shorthand form of operator pradeep_sn 2 1,776 May-20-2020, 06:53 PM
Last Post: buran
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 2,283 Apr-06-2020, 09:14 AM
Last Post: bowlofred
  Pass Arguments to Function phillyfa 2 2,058 Mar-27-2020, 12:05 PM
Last Post: phillyfa

Forum Jump:

User Panel Messages

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