Avoid passing too many argumentsWhat does ** (double star/asterisk) and * (star/asterisk) do for parameters?What can you use Python generator functions for?Which exception should I raise on bad/illegal argument combinations in Python?Short description of the scoping rules?How do I return multiple values from a function?How do I pass a variable by reference?“Least Astonishment” and the Mutable Default ArgumentRun a Python script from another Python script, passing in argumentsPython function as a function argument?Understanding the map functionDefine functions with too many arguments to abide by PEP8 standard
How can I fix cracks between the bathtub and the wall surround?
Pen test results for web application include a file from a forbidden directory that is not even used or referenced
What does なんだって mean in this case? 「そういう子なんだってだけで...」
I feel cheated by my new employer, does this sound right?
Why are JWST optics not enclosed like HST?
Why did the population of Bhutan drop by 70% between 2007 and 2008?
Is it alright to add scenes that don’t move the plot forwards much but develop relationships/character?
Where should I draw the line on follow up questions from previous employer
Do multi-engine jets need all engines with equal age to reduce asymmetry in thrust and fuel consumption arising out of deterioration?
Why is there no Disney logo in MCU movies?
Can a network vulnerability be exploited locally?
Wrong Stamping of UK Visa
Idiomatic way to create an immutable and efficient class in C++?
Convert shapefille to KML
How to copy the month value from one Date object to another?
How to handle inventory and story of a player leaving
How could a self contained organic body propel itself in space
What is Soda Fountain Etiquette?
How can weighted averages be calculated for a Dataset summarized with GroupBy
Why is "I let him to sleep" incorrect (or is it)?
Create a list of snaking numbers under 50,000
Why do IR remotes influence AM radios?
Is it unusual for a math department not to have a mail/web server?
Cauterizing a wound with metal?
Avoid passing too many arguments
What does ** (double star/asterisk) and * (star/asterisk) do for parameters?What can you use Python generator functions for?Which exception should I raise on bad/illegal argument combinations in Python?Short description of the scoping rules?How do I return multiple values from a function?How do I pass a variable by reference?“Least Astonishment” and the Mutable Default ArgumentRun a Python script from another Python script, passing in argumentsPython function as a function argument?Understanding the map functionDefine functions with too many arguments to abide by PEP8 standard
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have the following code where I need to pass all the arguments from one function to another. I want to know a way avoid the long list of arguments. I only "know" there is "*" and "**" in python, but I have never used them before.
# definition
def TestCase(test_name, op_type, input_shapes, op_args, run_mode):
# all those arguments are unchanged before passing to
# "add_tester"
...
# another function, the long list of arguments doesn't look
# good to me
add_tester("c2", test_name, input_shapes, op_args, run_mode, benchmark_func)
# Call TestCase
TestCase(
test_name='mm',
op_type='MM',
input_shapes=input_shapes,
op_args='trans_a': trans_a, 'trans_b': trans_b,
run_mode=run_mode)
python
add a comment |
I have the following code where I need to pass all the arguments from one function to another. I want to know a way avoid the long list of arguments. I only "know" there is "*" and "**" in python, but I have never used them before.
# definition
def TestCase(test_name, op_type, input_shapes, op_args, run_mode):
# all those arguments are unchanged before passing to
# "add_tester"
...
# another function, the long list of arguments doesn't look
# good to me
add_tester("c2", test_name, input_shapes, op_args, run_mode, benchmark_func)
# Call TestCase
TestCase(
test_name='mm',
op_type='MM',
input_shapes=input_shapes,
op_args='trans_a': trans_a, 'trans_b': trans_b,
run_mode=run_mode)
python
you can pass a dict. For example:TestCase(**Your_DICT)
and like this you'll unpack tyour dict and use it's kyes & values inside your class as arguments.
– Chiheb Nexus
Mar 27 at 22:08
In general, you create a class/object that wraps all related parameters. refactoring.guru/smells/long-parameter-list
– cricket_007
Mar 27 at 22:19
add a comment |
I have the following code where I need to pass all the arguments from one function to another. I want to know a way avoid the long list of arguments. I only "know" there is "*" and "**" in python, but I have never used them before.
# definition
def TestCase(test_name, op_type, input_shapes, op_args, run_mode):
# all those arguments are unchanged before passing to
# "add_tester"
...
# another function, the long list of arguments doesn't look
# good to me
add_tester("c2", test_name, input_shapes, op_args, run_mode, benchmark_func)
# Call TestCase
TestCase(
test_name='mm',
op_type='MM',
input_shapes=input_shapes,
op_args='trans_a': trans_a, 'trans_b': trans_b,
run_mode=run_mode)
python
I have the following code where I need to pass all the arguments from one function to another. I want to know a way avoid the long list of arguments. I only "know" there is "*" and "**" in python, but I have never used them before.
# definition
def TestCase(test_name, op_type, input_shapes, op_args, run_mode):
# all those arguments are unchanged before passing to
# "add_tester"
...
# another function, the long list of arguments doesn't look
# good to me
add_tester("c2", test_name, input_shapes, op_args, run_mode, benchmark_func)
# Call TestCase
TestCase(
test_name='mm',
op_type='MM',
input_shapes=input_shapes,
op_args='trans_a': trans_a, 'trans_b': trans_b,
run_mode=run_mode)
python
python
asked Mar 27 at 22:05
ZackZack
4021 gold badge4 silver badges20 bronze badges
4021 gold badge4 silver badges20 bronze badges
you can pass a dict. For example:TestCase(**Your_DICT)
and like this you'll unpack tyour dict and use it's kyes & values inside your class as arguments.
– Chiheb Nexus
Mar 27 at 22:08
In general, you create a class/object that wraps all related parameters. refactoring.guru/smells/long-parameter-list
– cricket_007
Mar 27 at 22:19
add a comment |
you can pass a dict. For example:TestCase(**Your_DICT)
and like this you'll unpack tyour dict and use it's kyes & values inside your class as arguments.
– Chiheb Nexus
Mar 27 at 22:08
In general, you create a class/object that wraps all related parameters. refactoring.guru/smells/long-parameter-list
– cricket_007
Mar 27 at 22:19
you can pass a dict. For example:
TestCase(**Your_DICT)
and like this you'll unpack tyour dict and use it's kyes & values inside your class as arguments.– Chiheb Nexus
Mar 27 at 22:08
you can pass a dict. For example:
TestCase(**Your_DICT)
and like this you'll unpack tyour dict and use it's kyes & values inside your class as arguments.– Chiheb Nexus
Mar 27 at 22:08
In general, you create a class/object that wraps all related parameters. refactoring.guru/smells/long-parameter-list
– cricket_007
Mar 27 at 22:19
In general, you create a class/object that wraps all related parameters. refactoring.guru/smells/long-parameter-list
– cricket_007
Mar 27 at 22:19
add a comment |
3 Answers
3
active
oldest
votes
If you are interested about how to use * and ** notations look at the example:
def f(a, *args, **kwargs):
print("a: %s, args: %s, kwargs: %s" % (a,args, kwargs))
f(1, 2, 3, 4, b=5, c=6, d=7)
# output: A: 1, args: (2, 3, 4), kwargs: 'b': 5, 'c': 6, 'd': 7
What does ** (double star/asterisk) and * (star/asterisk) do for parameters? contains a good explanation on this things.
As for your code if you don't want to pass all your arguments one by one, you can use **kwargs for all your variables:
# definition
def TestCase(**kwargs):
# all those arguments are unchanged before passing to
# "add_tester"
...
# another function, the long list of arguments doesn't look
# good to me
add_tester("c2", kwargs)
# Call TestCase
TestCase(test_name='mm', op_type='MM', input_shapes=input_shapes, op_args='trans_a': trans_a, 'trans_b': trans_b, run_mode=run_mode)
But you will need to refer to your arguments as kwargs['test_name'], kwargs['op_type'], ...
and your arguments become optional, whoever calls your method will not see what actual arguments are expected.
add a comment |
Write a class, put in the parameters at __init__
, and use self
.
class TestCase:
def __init__(self, test_name, op_type, run_mode, benchmark_func):
self._test_name = test_name
self._op_type = op_type
self._run_mode = run_mode
self._benchmark_func = benchmark_func
# bunch of initiation code follows
# another function, the long list of arguments doesn't look
# good to me
def run_test(self, op_args, idk_what_this_is="c2"):
# access self._XX for the fields
A few notes:
- Be careful with naming conventions. Use lowercase with underscore for functions/methods.
- If you are doing conventional testing, consider existing frameworks like
nose
. There are lots of code pattern that you don't need to rewrite by doing so.
add a comment |
You can use global variables. Global variables are the one that are defined and declared outside a function and we need to use them inside a function. GeeksforGeeks has some examples: https://www.geeksforgeeks.org/global-local-variables-python/
There's also the config file. The canonical way to share information across modules within a single program is to create a special module (often called config or cfg). Just import the config module in all modules of your application; the module then becomes available as a global name. See python faq's: https://docs.python.org/3/faq/programming.html#how-do-i-share-global-variables-across-modules
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387187%2favoid-passing-too-many-arguments%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you are interested about how to use * and ** notations look at the example:
def f(a, *args, **kwargs):
print("a: %s, args: %s, kwargs: %s" % (a,args, kwargs))
f(1, 2, 3, 4, b=5, c=6, d=7)
# output: A: 1, args: (2, 3, 4), kwargs: 'b': 5, 'c': 6, 'd': 7
What does ** (double star/asterisk) and * (star/asterisk) do for parameters? contains a good explanation on this things.
As for your code if you don't want to pass all your arguments one by one, you can use **kwargs for all your variables:
# definition
def TestCase(**kwargs):
# all those arguments are unchanged before passing to
# "add_tester"
...
# another function, the long list of arguments doesn't look
# good to me
add_tester("c2", kwargs)
# Call TestCase
TestCase(test_name='mm', op_type='MM', input_shapes=input_shapes, op_args='trans_a': trans_a, 'trans_b': trans_b, run_mode=run_mode)
But you will need to refer to your arguments as kwargs['test_name'], kwargs['op_type'], ...
and your arguments become optional, whoever calls your method will not see what actual arguments are expected.
add a comment |
If you are interested about how to use * and ** notations look at the example:
def f(a, *args, **kwargs):
print("a: %s, args: %s, kwargs: %s" % (a,args, kwargs))
f(1, 2, 3, 4, b=5, c=6, d=7)
# output: A: 1, args: (2, 3, 4), kwargs: 'b': 5, 'c': 6, 'd': 7
What does ** (double star/asterisk) and * (star/asterisk) do for parameters? contains a good explanation on this things.
As for your code if you don't want to pass all your arguments one by one, you can use **kwargs for all your variables:
# definition
def TestCase(**kwargs):
# all those arguments are unchanged before passing to
# "add_tester"
...
# another function, the long list of arguments doesn't look
# good to me
add_tester("c2", kwargs)
# Call TestCase
TestCase(test_name='mm', op_type='MM', input_shapes=input_shapes, op_args='trans_a': trans_a, 'trans_b': trans_b, run_mode=run_mode)
But you will need to refer to your arguments as kwargs['test_name'], kwargs['op_type'], ...
and your arguments become optional, whoever calls your method will not see what actual arguments are expected.
add a comment |
If you are interested about how to use * and ** notations look at the example:
def f(a, *args, **kwargs):
print("a: %s, args: %s, kwargs: %s" % (a,args, kwargs))
f(1, 2, 3, 4, b=5, c=6, d=7)
# output: A: 1, args: (2, 3, 4), kwargs: 'b': 5, 'c': 6, 'd': 7
What does ** (double star/asterisk) and * (star/asterisk) do for parameters? contains a good explanation on this things.
As for your code if you don't want to pass all your arguments one by one, you can use **kwargs for all your variables:
# definition
def TestCase(**kwargs):
# all those arguments are unchanged before passing to
# "add_tester"
...
# another function, the long list of arguments doesn't look
# good to me
add_tester("c2", kwargs)
# Call TestCase
TestCase(test_name='mm', op_type='MM', input_shapes=input_shapes, op_args='trans_a': trans_a, 'trans_b': trans_b, run_mode=run_mode)
But you will need to refer to your arguments as kwargs['test_name'], kwargs['op_type'], ...
and your arguments become optional, whoever calls your method will not see what actual arguments are expected.
If you are interested about how to use * and ** notations look at the example:
def f(a, *args, **kwargs):
print("a: %s, args: %s, kwargs: %s" % (a,args, kwargs))
f(1, 2, 3, 4, b=5, c=6, d=7)
# output: A: 1, args: (2, 3, 4), kwargs: 'b': 5, 'c': 6, 'd': 7
What does ** (double star/asterisk) and * (star/asterisk) do for parameters? contains a good explanation on this things.
As for your code if you don't want to pass all your arguments one by one, you can use **kwargs for all your variables:
# definition
def TestCase(**kwargs):
# all those arguments are unchanged before passing to
# "add_tester"
...
# another function, the long list of arguments doesn't look
# good to me
add_tester("c2", kwargs)
# Call TestCase
TestCase(test_name='mm', op_type='MM', input_shapes=input_shapes, op_args='trans_a': trans_a, 'trans_b': trans_b, run_mode=run_mode)
But you will need to refer to your arguments as kwargs['test_name'], kwargs['op_type'], ...
and your arguments become optional, whoever calls your method will not see what actual arguments are expected.
answered Mar 27 at 22:44
andnikandnik
1,1079 silver badges19 bronze badges
1,1079 silver badges19 bronze badges
add a comment |
add a comment |
Write a class, put in the parameters at __init__
, and use self
.
class TestCase:
def __init__(self, test_name, op_type, run_mode, benchmark_func):
self._test_name = test_name
self._op_type = op_type
self._run_mode = run_mode
self._benchmark_func = benchmark_func
# bunch of initiation code follows
# another function, the long list of arguments doesn't look
# good to me
def run_test(self, op_args, idk_what_this_is="c2"):
# access self._XX for the fields
A few notes:
- Be careful with naming conventions. Use lowercase with underscore for functions/methods.
- If you are doing conventional testing, consider existing frameworks like
nose
. There are lots of code pattern that you don't need to rewrite by doing so.
add a comment |
Write a class, put in the parameters at __init__
, and use self
.
class TestCase:
def __init__(self, test_name, op_type, run_mode, benchmark_func):
self._test_name = test_name
self._op_type = op_type
self._run_mode = run_mode
self._benchmark_func = benchmark_func
# bunch of initiation code follows
# another function, the long list of arguments doesn't look
# good to me
def run_test(self, op_args, idk_what_this_is="c2"):
# access self._XX for the fields
A few notes:
- Be careful with naming conventions. Use lowercase with underscore for functions/methods.
- If you are doing conventional testing, consider existing frameworks like
nose
. There are lots of code pattern that you don't need to rewrite by doing so.
add a comment |
Write a class, put in the parameters at __init__
, and use self
.
class TestCase:
def __init__(self, test_name, op_type, run_mode, benchmark_func):
self._test_name = test_name
self._op_type = op_type
self._run_mode = run_mode
self._benchmark_func = benchmark_func
# bunch of initiation code follows
# another function, the long list of arguments doesn't look
# good to me
def run_test(self, op_args, idk_what_this_is="c2"):
# access self._XX for the fields
A few notes:
- Be careful with naming conventions. Use lowercase with underscore for functions/methods.
- If you are doing conventional testing, consider existing frameworks like
nose
. There are lots of code pattern that you don't need to rewrite by doing so.
Write a class, put in the parameters at __init__
, and use self
.
class TestCase:
def __init__(self, test_name, op_type, run_mode, benchmark_func):
self._test_name = test_name
self._op_type = op_type
self._run_mode = run_mode
self._benchmark_func = benchmark_func
# bunch of initiation code follows
# another function, the long list of arguments doesn't look
# good to me
def run_test(self, op_args, idk_what_this_is="c2"):
# access self._XX for the fields
A few notes:
- Be careful with naming conventions. Use lowercase with underscore for functions/methods.
- If you are doing conventional testing, consider existing frameworks like
nose
. There are lots of code pattern that you don't need to rewrite by doing so.
answered Mar 27 at 22:34
PM HuiPM Hui
1566 bronze badges
1566 bronze badges
add a comment |
add a comment |
You can use global variables. Global variables are the one that are defined and declared outside a function and we need to use them inside a function. GeeksforGeeks has some examples: https://www.geeksforgeeks.org/global-local-variables-python/
There's also the config file. The canonical way to share information across modules within a single program is to create a special module (often called config or cfg). Just import the config module in all modules of your application; the module then becomes available as a global name. See python faq's: https://docs.python.org/3/faq/programming.html#how-do-i-share-global-variables-across-modules
add a comment |
You can use global variables. Global variables are the one that are defined and declared outside a function and we need to use them inside a function. GeeksforGeeks has some examples: https://www.geeksforgeeks.org/global-local-variables-python/
There's also the config file. The canonical way to share information across modules within a single program is to create a special module (often called config or cfg). Just import the config module in all modules of your application; the module then becomes available as a global name. See python faq's: https://docs.python.org/3/faq/programming.html#how-do-i-share-global-variables-across-modules
add a comment |
You can use global variables. Global variables are the one that are defined and declared outside a function and we need to use them inside a function. GeeksforGeeks has some examples: https://www.geeksforgeeks.org/global-local-variables-python/
There's also the config file. The canonical way to share information across modules within a single program is to create a special module (often called config or cfg). Just import the config module in all modules of your application; the module then becomes available as a global name. See python faq's: https://docs.python.org/3/faq/programming.html#how-do-i-share-global-variables-across-modules
You can use global variables. Global variables are the one that are defined and declared outside a function and we need to use them inside a function. GeeksforGeeks has some examples: https://www.geeksforgeeks.org/global-local-variables-python/
There's also the config file. The canonical way to share information across modules within a single program is to create a special module (often called config or cfg). Just import the config module in all modules of your application; the module then becomes available as a global name. See python faq's: https://docs.python.org/3/faq/programming.html#how-do-i-share-global-variables-across-modules
answered Mar 29 at 0:47
user222216user222216
621 silver badge11 bronze badges
621 silver badge11 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387187%2favoid-passing-too-many-arguments%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
you can pass a dict. For example:
TestCase(**Your_DICT)
and like this you'll unpack tyour dict and use it's kyes & values inside your class as arguments.– Chiheb Nexus
Mar 27 at 22:08
In general, you create a class/object that wraps all related parameters. refactoring.guru/smells/long-parameter-list
– cricket_007
Mar 27 at 22:19