python - 为什么 VSCode 不能为我的对象提供代码补全?

为什么 VS Code 不弄清楚如何完成方法和属性?当我写:ai_game。没有任何反应,没有出现方法或属性列表

import pygame
 
class Ship:
    """A class to manage the ship."""
    
    def __init__(self, ai_game):
        """Initialize the ship and set its starting position."""
        self.screen = ai_game.screen
        self.settings = ai_game.settings
        self.screen_rect = ai_game.screen.get_rect()

回答1

因为它不知道 ai_game 是什么。它可以是任何类型。为其添加类型注释,例如:

def __init__(self, ai_game: float):

好吧,在这个例子中,我很确定你没有浮点数,但它会帮助 VS Code 知道 ai_game 实际上是什么类型并帮助你使用它的方法..

相似文章

随机推荐

最新文章