0

I am looping a array of objects in python,

class Day():
  def __init__(self,date,month,year):
    self.date = date
    self.month = month
    self.year = year

days = [Day(12,3,23),Day(13,3,23),Day(14,3,23)]

for i,val in enumerate(days):
  print(val.date)

Now I need to specify the type of val as Day,so it knows what its attributes are inside the loop,and throw suggestions while typing.

I am thinking of something like

val:Day() for i,val in enumerate(days):

But this throws errors,Is there any way to specify datatype explicitly

EDIT : The use case is just not to add it for readability but object's attributes would appear in suggestion throughout the loop ,if variable type is known,none of the mentioned methods in How do I annotate types in a for-loop? work for the suggestion part

  • Please check this - https://stackoverflow.com/questions/41641449/how-do-i-annotate-types-in-a-for-loop – Madhuri Kadam Feb 01 '23 at 13:06
  • @MadhuriKadam please use the flagging system to propose duplicates. That makes it easier for others with close-vote privileges to vote to close the question as a duplicate. (I have privileges to close it by myself, so I did that.) – Karl Knechtel Feb 01 '23 at 13:18
  • I added the question after trying out that link, the syntax mentioned there like i:int ,but that couldn't be done for class objects,it throws out errors,that's the need for this question – Kaviya Subramanian Feb 02 '23 at 08:19

0 Answers0