【问题标题】:Python methods do not show up in user defined functionsPython 方法不会出现在用户定义的函数中
【发布时间】:2019-05-28 21:53:28
【问题描述】:

创建用户定义函数时,内置方法不会作为选项弹出。

在文件的主体中,方法显示没有问题。只有当您创建一个函数时,它们才会显示出来。

def strip_punctuation(astring):
    #punctuation_chars = ["'", '"', ",", ".", "!", ":", ";", '#', '@']
    astring.replace()
    return (astring)

# Main file
my_string = "Word's!"
my_string.replace()

我希望 VS Code 在用户定义的函数和函数外部都有相同的行为。

【问题讨论】:

  • 因为您的文本编辑器无法知道 astring 应该是 str 对象。 Python 是一种动态类型语言,没有什么能阻止您将任何对象传递给strip_punctuation,那么您如何期望它知道它应该建议字符串方法而不是列表方法?在 my_string = "Words'!" 的情况下可以,因为您直接分配了 str 文字。您应该考虑使用类型注释。

标签: python python-3.x visual-studio-code


【解决方案1】:

如果你用类型注释重写你的代码,你应该得到你想要的:

def strip_punctuation(astring: str) -> str:
    astring.replace()
    return astring

【讨论】:

    猜你喜欢
    • 2018-10-20
    • 2021-12-17
    • 2019-11-06
    • 2019-03-06
    • 2017-07-09
    • 1970-01-01
    • 2012-03-30
    • 1970-01-01
    • 2017-05-16
    相关资源
    最近更新 更多