0

What does the last part of this code do exactly? Also why is self.name equal to name? here is the code below:

class Shark:
    def __init__(self, name):
        self.name = name

    def swim(self):
        print(self.name + " is swimming.")

    def be_awesome(self):
        print(self.name + " is being awesome.")

def main():
    sammy = Shark("Sammy")
    sammy.be_awesome()
    stevie = Shark("Stevie")
    stevie.swim()

if __name__ == "__main__":
  main()
John Gordon
  • 29,573
  • 7
  • 33
  • 58

2 Answers2

0

Firs part of your question is answered here

Second question name is the argument passed when creating a instance of your class. However self.name is a instance variable. The value of this is set to name. Read more about python classes here

BrandyBerry
  • 136
  • 4
0

The last part of the code:

if __name__ == "__main__":
  main()

simply means that when this particular script is run as the entry point file, it will execute the main() function. If the contents are imported by another script, it will ignore the if statement.

self.name = name

assigns the input variable name to the instance variable of the class, in this case it is also called name