Home > Software engineering >  Visual Studio Code doesn't show the methods from an object in an array python
Visual Studio Code doesn't show the methods from an object in an array python

Time:04-01

I'm learning oop in python after learning it in Java. It happens that i'm trying to create an array from my class Person which will contain Person objects within. When I create a single object Intellisense shows the methods available:

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.

But when I make an array with Person objects within it and I try to use the methods calling the object from the array the methods just doesn't appear.

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.

Does anyone know why is this happening? :C

CodePudding user response:

You can add type hint for people.

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") 
  • Related