What is a static attribute versus a dynamic attribute in classes?

What is a static attribute versus a dynamic attribute in classes?


So yesterday I learned (but I was too busy at and after work) to write it about what the difference between a static attribute or a dynamic attribute in a class is.

So let us define a class.

class LanaDelReySong:

    artist = "Lana del Rey"
 
    def __init__(self, name: str, artist: str):
    self.name  = name

So in this instance is:
  • The artist name, or "Lana del Rey", is a STATIC CLASS ATTRIBUTE.
BUT THEN:

yosemite = LanaDelReySong(name="Yosemite")

dance_tile_we_die = LanaDelReySong(name="Dance Til We Die")

white_dress = LanaDelReySong(name="White Dress")

All three of these song names are DYNAMIC CLASS ATTRIBUTES.

THEY CHANGE WITH EVERY INSTANCE OF THE CLASS.

SO NO MATTER WHAT, A LANA DEL REY SONG ALWAYS HAS THE ARTIST OF LANA.

BUT THE NAMES OF THE SONG TITLES WILL CHANGE EVERY TIME.


Pictures of coding interspersed with one another, in blue and purple. Text on drawings of pieces of paper on top. Text reads: Static vs. dynamic class atrributes. A static attribute stays the same across all instances of a class. A dynamic attribute can change with every new instance of a class.








Comments