Number of attributes in a Python class

Asked

Viewed 63 times

0

Is there any "rule"/convention for the amount of attributes in a class?

Example: In a game, a Character(class) has more than 30 attributes between name, level, strength, etc.

With all the attributes encapsulated the class ends up being gigantic. This is normal or is a bad practice?

1 answer

1


The rule is common sense, and mainly, the good programming practices.

To object orientation is a form of programming that enables developers to think about things like work in real life: objects. 2

With techniques such as composition, inheritance and polymorphism, one can achieve a better abstraction:

class Armamento:
   dano = 1
...
class Punhal(Armamento):
    def apunhalar() 
...
class Rifle(Armamento) 
    def atirar()
...
class Agente:
    nivel = 10
    forca = 2
    arma = Rifle
...
class Personagem(Agente): 
    nome = 'ficticio'
    def aguardar_entrada()
...
class Inimigo(Agente):
    def cacular_ataque() 
...

The lack of this abstraction should result in greater effort/operational cost for understanding and maintenance of the code, affecting the software quality.

Even if I exaggerate, I say yes, it is a bad practice a class with more than 30 attributes, with many exceptions. 9

Look at object orientation, or necessary for its application, your way.

  • 1

    Excellent understanding of my question and objectivity in the reply. Congratulations and thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.