dshimer 136
Recognized Expert New Member
In the simplest terms...
I have a function that returns 3 values - >>> def sendnum():
-
... return 1,2,3
and a function that takes 3 arguments. - >>> def receive(a=4,b=5,c=6):
-
... print a,b,c
They of course do their individual jobs - >>> sendnum()
-
(1, 2, 3)
-
>>> receive(11,12,13)
-
11 12 13
Is there any way to code this so that the sending function doesn't send it as a list, or that the receiving function sees it as 3 different values? So instead of - >>> receive(sendnum())
-
(1, 2, 3) 5 6
I would get - >>> receive(sendnum())
-
1 2 3
4 2194 bvdet 2,851
Recognized Expert Moderator Specialist
In the simplest terms...
I have a function that returns 3 values - >>> def sendnum():
-
... return 1,2,3
and a function that takes 3 arguments. - >>> def receive(a=4,b=5,c=6):
-
... print a,b,c
They of course do their individual jobs - >>> sendnum()
-
(1, 2, 3)
-
>>> receive(11,12,13)
-
11 12 13
Is there any way to code this so that the sending function doesn't send it as a list, or that the receiving function sees it as 3 different values? So instead of - >>> receive(sendnum())
-
(1, 2, 3) 5 6
I would get - >>> receive(sendnum())
-
1 2 3
- >>> receive(*sendnum())
-
1 2 3
-
>>>
dshimer 136
Recognized Expert New Member
Fantastic, what is terminology for this, so I can study up on it? I have tried it in an system which embeds python to access CAD functions, so I have no control over the supplied objects. This works perfect.
Thanks - >>> receive(*sendnum())
-
1 2 3
-
>>>
bvdet 2,851
Recognized Expert Moderator Specialist
Fantastic, what is terminology for this, so I can study up on it? I have tried it in an system which embeds python to access CAD functions, so I have no control over the supplied objects. This works perfect.
Thanks
A function can accept a variable number of arguments if an asterisk (*) precedes the last argument in an argument list: - >>> def sample(s, *args):
-
... print s
-
... for item in args:
-
... print item
-
...
-
>>> s = 'A string'
-
>>> sample(s, 'This', 'is', 'a', 'test')
-
A string
-
This
-
is
-
a
-
test
-
>>> aList = ('This', 'is', 'a', 'test')
-
>>> sample(s, aList)
-
A string
-
('This', 'is', 'a', 'test')
-
>>> sample(s, *aList)
-
A string
-
This
-
is
-
a
-
test
-
>>>
A function can also accept a variable number of keyword arguments if '**' precedes the last argument in an argument list: - >>> def samplekw(**kargs):
-
... for key in kargs:
-
... print key, kargs[key]
-
...
-
>>> samplekw(**{'key1': 1, 'key2': 100})
-
key2 100
-
key1 1
-
>>> samplekw(key1=1, key2=100)
-
key2 100
-
key1 1
-
>>>
Combined: - >>> def sample(s, *args, **kargs):
-
... print s
-
... for item in args:
-
... print item
-
... for key in kargs:
-
... print key, kargs[key]
-
...
-
>>> sample(s, *aList, **{'key1': 1, 'key2': 100})
-
A string
-
This
-
is
-
a
-
test
-
key2 100
-
key1 1
-
>>>
bartonc 6,596
Recognized Expert Expert
Fantastic, what is terminology for this, so I can study up on it? I have tried it in an system which embeds python to access CAD functions, so I have no control over the supplied objects. This works perfect.
Thanks
Mark Lutz calls it "varargs" on p 338 of
7. More argument matching examples. Here is the sort of interaction you should get, along with comments that explain the matching that goes on: -
def f1(a, b): print a, b # normal args
-
-
def f2(a, *b): print a, b # positional varargs
-
-
f3(a, **b): print a, b # keyword varargs
-
-
f4(a, *b, **c): print a, b, c # mixed modes
-
-
f5(a, b=2, c=3): print a, b, c # defaults
-
-
f6(a, b=2, *c): print a, b, c # defaults + positional varargs
-
% python
-
>>> f1(1, 2) # matched by position (order matters)
-
1 2
-
>>> f1(b=2, a=1) # matched by name (order doesn't matter)
-
1 2
-
>>> f2(1, 2, 3) # extra positionals collected in a tuple
-
1 (2, 3)
-
>>> f3(1, x=2, y=3) # extra keywords collected in a dictionary
-
1 {'x': 2, 'y': 3}
-
>>> f4(1, 2, 3, x=2, y=3) # extra of both kinds
-
1 (2, 3) {'x': 2, 'y': 3}
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Andrae Muys |
last post by:
Found myself needing serialised access to a shared generator from
multiple threads. Came up with the following
def serialise(gen):
lock = threading.Lock()
while 1:
lock.acquire()
try:
next...
|
by: Darren Dale |
last post by:
Hello,
def test(data):
i = ? This is the line I have trouble with
if i==1: return data
else: return data
a,b,c,d = test()
|
by: John Smith |
last post by:
I have a user assigned multiple roles and a role can be inherited from
multiple parents (see below). How do I answer such questions as "How
many roles does the user belongs to?"
I answered the...
|
by: Roland Hall |
last post by:
Is there a way to return multiple values from a function without using an
array? Would a dictionary object work better?
--
Roland Hall
/* This information is distributed in the hope that it...
|
by: randy.p.ho |
last post by:
Using JDBC, is there a way to call a stored procedure with multiple
return values? Thanks.
| |
by: Nikolay Petrov |
last post by:
How can I return multiple values from a custom function?
TIA
|
by: aleksandar.ristovski |
last post by:
Hello all,
I have been thinking about a possible extension to C/C++ syntax.
The current syntax allows declaring a function that returns a value:
int foo();
however, if I were to return...
|
by: ADezii |
last post by:
The incentive for this Tip was an Article by the amazing Allen Browne - I considered it noteworthy enough to post as The Tip of the Week in this Access Forum.
Original Article by Allen Browne
...
|
by: satyanarayan sahoo |
last post by:
Can we write a method which returns multiple values?
|
by: mukeshrasm |
last post by:
Hi
I am using AJAX to display the value in selection/list box. this code is working fine in Firefox Mozila browser but it is not working in Internet Explorer so please tell me how this will work...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |