Python Forum
How to access arguments by name from function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to access arguments by name from function
#1
Hi,

I have function in which I am passing multiple lists. Now, I want to access (or assign to another variable) the lists (given as arguments in function) by its name. I tried below code, but its giving error.

#%% passing multiple lsit to function
def my_func(list1,list2,list3):
    return list1,list2,list3
p1 = my_func(list1 = ['BNKL', 'KPUI', 'HYNI'],\
             list2 = ['Ty'],\
                  list3 = [6, 7, 8])
print(" The first list : ", p1[0])
print(" The second list : ", p1[list1])
l_l1 = p1[1]
Error:

The first list :  ['BNKL', 'KPUI', 'HYNI']
Traceback (most recent call last):

  File "<ipython-input-2-30d2f38dbdd7>", line 6, in <module>
    print(" The second list : ", p1[list1])

NameError: name 'list1' is not defined
Reply
#2
Names defined in the function are only available in the function. If you need them outside the function, return a dictionary:

def foo(list1, list2, list3):
    return {'list1': list1, 'list2': list2, 'list3': list3}
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
#%% passing multiple lsit to function
def my_func(list1,list2,list3):
    return {'list1': list1,'list2':list2,'list3': list3}
p1 = my_func(list1 = ['BNKL', 'KPUI', 'HYNI'],\
             list2 = ['Ty'],\
                  list3 = [6, 7, 8])
print(" The second list : ", p1['list2'])
print(" The first list : ", p1[0])
Error: Can't we acess by index?

The second list : ['Ty']
Traceback (most recent call last):

File "<ipython-input-7-9fcbb5cddfc7>", line 6, in <module>
print(" The first list : ", p1[0])

KeyError: 0
Reply
#4
Not with a dictionary. You could use a named tuple, but before I went that far I would ask if I really needed to access by both.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Does it seem if we give in list format, then we can not access list by index?
Reply
#6
You can get it by index if you return the values as a nested list

def my_func(list1,list2,list3):
    return [list1,list2,list3]
p1 = my_func(list1 = ['BNKL', 'KPUI', 'HYNI'],\
             list2 = ['Ty'],\
                  list3 = [6, 7, 8])

print(" The first list : ", p1[0])
Reply
#7
Sir/Madam,
my apologies, I am still confused, for example: from the nested list, we can not access by name, but only access by index. In the previous answer below, we can only access by name. This conclusion is correct? or can we make it flexible that we can either access by index or by name?
def my_func(list1,list2,list3):
    return {'list1': list1,'list2':list2,'list3': list3}
Reply
#8
(Sep-19-2019, 03:06 PM)SriRajesh Wrote: can we make it flexible that we can either access by index or by name?

As I said, you would need to use a namedtuple. But why do you need to access it both ways? It creates an inconsistency, and inconsistencies cause problems down the road. It might be better to change how you are using the data than to change the data.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  calling external function with arguments Wimpy_Wellington 7 1,344 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  'namespace' shorthand for function arguments? shadowphile 5 2,541 Aug-11-2021, 09:02 PM
Last Post: shadowphile
  Checking the number of arguments a function takes Chirumer 3 2,111 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Possible to dynamically pass arguments to a function? grimm1111 2 2,125 Feb-21-2021, 05:57 AM
Last Post: deanhystad
  How to pass multiple arguments into function Mekala 4 2,381 Jul-11-2020, 07:03 AM
Last Post: Mekala
  How to give a name to function arguments in C-API? WonszZeczny 0 1,313 Jun-22-2020, 10:20 AM
Last Post: WonszZeczny
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 2,208 Apr-06-2020, 09:14 AM
Last Post: bowlofred
  Pass Arguments to Function phillyfa 2 1,982 Mar-27-2020, 12:05 PM
Last Post: phillyfa
  Function with many arguments, with some default values medatib531 3 2,529 Mar-14-2020, 02:39 AM
Last Post: medatib531
  Using function *args to multiply multiple arguments allusernametaken 8 5,951 Nov-20-2019, 12:01 AM
Last Post: allusernametaken

Forum Jump:

User Panel Messages

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