0

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"

1
  • What does get_quest_guide_char do with the marker_sector variable ? You'll have the option of just making it None or just not passing all together with an actual if statement, but the first one will depend if get_quest_guide_char will handle a marker_sector=None properly. Commented May 19, 2020 at 5:30

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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=.

2 Comments

This always needs a second argument. For me, I was trying to do if the if check fails, then there is not even second argument. So far best is this: get_quest_guide_char("isabelle", **{"marker_sector": "right_bottom" for x in [1] * (game.location == "school_english_class")})
This line **{"marker_sector": "right_bottom" for x in [1] * (game.location == "school_english_class")} will pass the function {"marker_sector": []} that same as marker_sector = [] so you replace in my answer None with [] and it will be the same It's not the best practice to pass empty list to function.
0

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)

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.