Best practices with class attributes, objects, and methods

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.


class Duck:
    sound = "quack"
    walking = "like a duck"
    def quack(self):
    def walk(self):
donald = Duck()

or B:

class Animal:
    def init(self, sound, walk, food):
        self.sound = sound
        self.walk = walk = food
    def describe(self):

Duck = Animal("quack", "waddle", "corn")

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!

I find it easiest to format code using Markdown fenced-code:

class A:
    def __init__(self, ...):

In terms of your question, it seems like A is specificity by inheritance (although you haven’t defined a base, abstract or otherwise, Animal class), while B is specificity by composition. Python really allows either (especially with the new typing.Protocol in Python 3.8), so at the end of the day I would suggest you do the one that takes the least time to develop and test.

1 Like