Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

How to use methods in Python

class Song(object):
    
    def __init__ (self, lyrics):
        self.lyrics = lyrics
        
    def sing_me_a_song(self):
        for line in self.lyrics:
            print(line)
            
happy_bday = Song(["Happy birthday to you",
                   "I don't want to get sued",
                   "So I'll stop right there"])
                   
bulls_on_parade = Song(["They rally around tha family",
                        "With pockets full of shells"])
                        

testing = "I am testing a coding exercise"

print(testing)

# Compare the following two notice what the square brackets do to the formatting of the output
testing = Song(testing)
testing = Song([testing])

happy_bday.sing_me_a_song()

bulls_on_parade.sing_me_a_song()

testing.sing_me_a_song()

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.