0

Classes in python

class student: cars=12 harry=student() harry.age=26 print(harry.__dict_) why does not this program print cars =12 too instead of printing only age=26 ?? but , print(harry.cars) will print 12.

27th May 2021, 6:07 PM
kushal
2 Réponses
+ 2
because object members and object attributes are two different things: attributes are accessed by dot notation, while members are accessed by square bracket notation (as indices in list, or keys in dict). an instance __dict__ attribute store only all members of it (by key/value pairs). you can get more information about that by reading this stackoverflow discuss: https://stackoverflow.com/questions/30250282/whats-the-difference-between-the-square-bracket-and-dot-notations-in-JUMP_LINK__&&__python__&&__JUMP_LINK
27th May 2021, 6:29 PM
visph
visph - avatar
0
You need to use def __init__(self, cars, age): self.cars=12 self.age=26
2nd Jun 2022, 3:21 PM
Chris
Chris - avatar