Reputation: 173
Is there any way to turn this psuedocode into real code?
get_quest_guide_char("isabelle", if game.location == "school_english_class": marker_sector="right_bottom"))
so marker_sector="right_bottom" only becomes the second argument based on the if check.
If the if check fails, the only argument passed is "isabelle"
Upvotes: 0
Views: 977
Reputation: 1516
Like the other answers before mine, this technically still passes at least two arguments. This is because function overloading isn't really a thing in Python, (having multiple functions with the same name). If you want a variable number of parameters, you can use *args
and/or **kwargs
when calling the function, but your function would still technically have to be defined with multiple arguments:
def get_quest_guide_char(character, *args, **kwargs):
# ...
# or in your case
def get_quest_guide_char(character, *args):
# ...
Or more explicitly:
def get_quest_guide_char(character, marker_sector=None):
# ...
This will let you do both:
get_quest_guide_char("isabelle", marker_sector)
# and
get_quest_guide_char("isabelle")
Given that marker_sector
depends on game.location
, you could alternatively create another function that gives you the marker_sector
, and simply pass that into your function regardless:
def get_marker_sector(location):
if (location == "school_english_class"):
marker_sector = "right_bottom"
else:
# ...
return marker_sector
marker_sector = get_marker_sector(game.location)
get_quest_guide_char("isabelle", marker_sector):
Alternatively, you could just pass in game.location
to your get_quest_guide_char
function:
def get_quest_guide_char(character, location):
if (location == "school_english_class"):
# ...
else:
# ...
get_quest_guide_char("isabelle", game.location)
Either way, I hope you're not intending on using marker_sector
as a global variable inside any of your functions. Mutable global variables are asking for trouble.
Upvotes: 0
Reputation: 568
Function/Methods are used to pass arguments based on their declaration and usage. Evaluation or doing any operation during declaration or usage is a bad approach and design.
What's the harm in using var-positional arguments i.e *args and *kwargs? The code should be simplified and not complicate. On top of that, think in terms of Unit testing your code function/code? Or may be you're not interested to unit test. Also I would recommend to go through PEP-0362
You can do your evaluation in if condition and then pass it as argument. In your case you could simply do like below:
Your function declaration:
def get_quest_guide_char(name: str, **kwargs):
if 'marker_sector' in kwargs:
market_sector = kwargs.get('marker_sector')
If you need to pass multiple other parameters, you can iterate over the kwargs dict:
def get_quest_guide_char(name: str, **kwargs):
for key, value in kwargs.items():
if key == 'marker_sector':
market_sector = value
Usage:
if value == "school_english_class":
get_quest_guide_char(name="isabelle", marker_sector="right_bottom")
else:
get_quest_guide_char(name="isabelle")
NOTE: I used named parameter for value 'isabelle' to make things easier, you can choose to omit or use.
Upvotes: 1
Reputation: 1404
It's probably better to just build your extra parameter outside of the method call, it makes it easier to read specially if the method parameters starts increasing.
params = {}
if game.location == "school_english_class":
params['mark_sector'] = "right_bottom"
get_quest_guide_char("isabelle", **params)
Upvotes: 0
Reputation: 4472
You can do
get_quest_guide_char("isabelle", marker_sector="right_bottom" if game.location == "school_english_class" else None))
This will pass to the function "right_bottom"
only if game.location == "school_english_class"
else it will pass None and then handle None in the function.
If you function defined as get_quest_guide_char(isabelle_value, marker_sector)
then you can use just "right_bottom" if game.location == "school_english_class" else None
without the marker_sector=
.
Upvotes: 0