简体   繁体   English

是否可以在没有参数的情况下调用需要参数的函数? 在蟒蛇上

[英]Is it possible to call a function that needs a parameter, without it? On python

I'm learning to code on Python 3.x using Exercism.我正在学习使用 Exercism 在 Python 3.x 上编码。 I have to code a conditional (on a secondary file) to return a string depending on a word sent from a main function:我必须编写一个条件(在辅助文件上)以根据从主函数发送的单词返回一个字符串:

User function:用户功能:

def two_fer(name):
    if name:
        string = "One for " + name + ", one for me."
    else:
        string = "One for you, one for me."
    return string

Main function:主功能:

class TwoFerTest(unittest.TestCase):
    def test_no_name_given(self):
        self.assertEqual(two_fer(), 'One for you, one for me.')

    def test_a_name_given(self):
        self.assertEqual(two_fer("Alice"), "One for Alice, one for me.")

    def test_another_name_given(self):
        self.assertEqual(two_fer("Bob"), "One for Bob, one for me.")

The problem is that , for the first condition in the main function, it calls two_fer() with no conditional, which results on a fail.问题是,对于主函数中的第一个条件,它调用two_fer()没有条件,这导致失败。

Main function is supposed to not be modified, is it any way to solve the problem only by the user function?主函数应该不会被修改,有没有办法只通过用户函数来解决问题?

Thanks in advance.提前致谢。

You can change:你可以改变:

def two_fer(name):

to:到:

def two_fer(name=None):

which will make name None by default默认情况下,这将使 name None

If you give name a default value of "you" , you can call the function without an explicit argument and get rid of the if statement altogether.如果你给name一个默认值"you" ,你可以在没有显式参数的情况下调用该函数完全摆脱if语句。

def two_fer(name="you"):
    return "One for " + name + ", one for me"

or better yet,或者更好,

def two_fer(name="you"):
    return "One for {}, one for me".format(name)

You can use a default parameter ie None and check whether the function received any parameter or not.您可以使用默认参数即 None 并检查函数是否收到任何参数。

User Function:用户功能:

def two_fer(name = None):
    if name != None:
        string = "One for " + name + ", one for me."
    else:
        string = "One for you, one for me."
    return string

The above code will execute the else part if the method two_fer is called without any parameters.如果在没有任何参数的情况下调用方法 two_fer ,则上述代码将执行 else 部分。

Use a default parameter like following使用如下所示的默认参数

def two_fer(name=None):
    if name:
        s = "One for " + name + ", one for me."
    else:
        s = "One for you, one for me."
return s

Also a tip, avoid assigning variable names as keywords for eg string in your case as it is a standard library and it is just a good practice to avoid it.还有一个提示,避免将变量名指定为例如字符串的关键字,因为它是一个标准库,避免它只是一个好习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可以在不使用括号的情况下调用单参数Python函数吗? - Possible to call single-parameter Python function without using parentheses? 在Python中,是否可以接受函数调用模板作为参数? - In Python, is it possible to accept a function call template as a parameter? 是否可以使用模板中的参数调用python函数? [姜戈] - Is it possible to call a python function with parameter from template? [Django] 在Python中是否可以在函数调用中设置多个参数的最后一个参数? - Is it in Python possible to set the last parameter of several in a function call? 调用 Python function 作为参数传递而不知道它是否需要任何参数 - Call Python function passed as parameter without knowing if it takes any parameters Django,调用同一个视图function有无参数 - Django, call the same view function with and without parameter 差异调用函数与星号参数和没有 - Difference call function with asterisk parameter and without 在python中调用没有括号的函数 - call function without parenthesis in python 是否可以使用 Python 中的装饰器向 function 添加参数? - Is it possible to add a parameter to a function with a decorator in Python? python:从输入中调用带有参数的函数 - python: call a function with parameter from input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM