1

I'm reading a Python Programming text where it's creating a scene within a game with the following code:

class Death(Scene):

    quips = [
        "You died.  You kinda suck at this.",
        "Your Mom would be proud...if she were smarter.",
        "Such a luser.",
        "I have a small puppy that's better at this.",
        "You're worse than your Dad's jokes."

    ]

    def enter(self):
        print(Death.quips[randint(0, len(self.quips)-1)])

My question is just over the last line. What's the difference when using a specific class name, in this case Death.quips as opposed to self.quips. The latter is how I've always seen it but it's used differently here. My first thought was maybe it bypasses needing to first create an object, but in this example self.quips requires the object anyways.

2 Answers 2

1

Essentially, they are accomplishing the same thing in the scenario. Death.quips is calling to the quips attribute of the class. Using self.quips is referring to the specific object of the class. You can change attributes on objects as well.

For example, if you changed Death.quips to self.quips you could do the following:

>>> a = Death()
>>> a.enter()
Such a luser.
>>> a.quips = [
        "a",
        "b",
        "c",
        "d",
        "e"

    ]
>>> a.enter()
c
>>> 

But, if you were to keep it Death.quips, this would be the result:

>>> a = Death()
>>> a.enter()
You died.  You kinda suck at this.
>>> a.quips = [
        "a",
        "b",
        "c",
        "d",
        "e"

    ]
>>> a.enter()
Such a luser.
>>> 

Self refers to the attributes of the instantiated object, while Death is referring to the unchanged attribute of the overall class.

I hope this helps and makes sense

Sign up to request clarification or add additional context in comments.

Comments

1

in this you can directly access the quips list as x = Death.quips without intialise it.

if you used self then you need to intialise the class first (using def __init__()) and in that you need to define quips. then you could access it as

x = Death()
quips_ = x.quips

else to directly access it, you needed to add the @staticmethod before a class method which provide u the list

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.