Visual Studio 代码未显示数组中对象的方法 python
Visual Studio Code doesn't show the methods from an object in an array python
我在 Java 学了 oop 之后又在 python 学了。碰巧我正在尝试从我的 class Person 创建一个数组,其中将包含 Person 对象。
当我创建单个对象时,Intellisense 会显示可用的方法:
class Person:
name: str
def set_name(self, name: str):
self.name = name
person1 = Person()
person1.set_name("John") #<--- Here it shows the custom method i made.
但是当我创建一个包含 Person 对象的数组并尝试使用从数组调用对象的方法时,这些方法就不会出现。
class Person:
name: str
def set_name(self, name: str):
self.name = name
people = []
for i in range (5):
people.append(Person())
people[0].set_name("John") #<---- After writting the dot the methods are empty,
#and when written it doesn't have the "color code" it should have.
有谁知道为什么会这样? :C
您可以为人物添加类型提示。
class Person:
name: str
def set_name(self, name: str):
self.name = name
people = []
for i in range (5):
people.append(Person())
people:list[Person] # add type hint here
people[0].set_name("John")
我在 Java 学了 oop 之后又在 python 学了。碰巧我正在尝试从我的 class Person 创建一个数组,其中将包含 Person 对象。 当我创建单个对象时,Intellisense 会显示可用的方法:
class Person:
name: str
def set_name(self, name: str):
self.name = name
person1 = Person()
person1.set_name("John") #<--- Here it shows the custom method i made.
但是当我创建一个包含 Person 对象的数组并尝试使用从数组调用对象的方法时,这些方法就不会出现。
class Person:
name: str
def set_name(self, name: str):
self.name = name
people = []
for i in range (5):
people.append(Person())
people[0].set_name("John") #<---- After writting the dot the methods are empty,
#and when written it doesn't have the "color code" it should have.
有谁知道为什么会这样? :C
您可以为人物添加类型提示。
class Person:
name: str
def set_name(self, name: str):
self.name = name
people = []
for i in range (5):
people.append(Person())
people:list[Person] # add type hint here
people[0].set_name("John")