I saw a tutorial in a video on how to use class attributes, and while I’m not new to coding, I’m new to Python, so my question is, which solution seems to be better? My apologies in advance on the blockquotes, I can’t seem to get the correct indentation or underscores to work in the formatting here. Edit: I seem to have fixed it with the >pre< tag.
A:
class Duck: sound = "quack" walking = "like a duck" def quack(self): print(self.sound) def walk(self): print(self.walking) donald = Duck() donald.quack() donald.walk()
or B:
class Animal: def init(self, sound, walk, food): self.sound = sound self.walk = walk self.food = food def describe(self): print(self.sound) print(self.walk) print(self.food) Duck = Animal("quack", "waddle", "corn") Duck.describe()
To me, ‘A’ feels like a class is being used as if it were an object, rather than a template/definition that other objects/instances can be created from, as well as being limited in the number of objects that can be created from it. ‘B’ seems to be more scalable, as well as more accessible if you wanted to create different spoken language variants of the object type, such as
DuckEsp = Animal("cuac cuac", "caminar", "maíz")It feels more like a class/object relationship than 'A'. I'm very interested in anyone's input on this. Thanks!