Quick actions

cmd+k|ctrl+k

Navigation

Languages

CLASS-OBJECT

Snippet info

Language

Python

Visibility

public

Author

aaron.lung.wai

Created

2022-11-23T14:28:02.615732Z

Updated

2022-11-23T14:28:02.615732Z

#Create a new class
class Welcome(object):
    def hello(self):
        print("Hello!")

#Create an object
x = Welcome()
x.hello()


y = Welcome()

print(type(x) == type(y))
print(type(x) is Welcome)
print(type(x) == Welcome)


# BUILD CLASS WITH INIT

class Welcome(object):
    def __init__(self):
        print("Python Tutorial created a new object")
    def hello(self):
        print("Hello!")

#Create an object
x = Welcome()


# BUILD CLASS WITH PARAMETER

class Welcome(object):
    def __init__(self, input):
        print(input)
    def hello(self):
        print("Hello!")

#Create an object
x = Welcome("I love Python Tutorial")


#Self

class Welcome(object):
    def __init__(self, what_to_say):
        self.set_sentence(what_to_say)
    def set_sentence(self, what_to_say):
        self.sentence = what_to_say.upper()
    def say(self):
        print(self.sentence)

#create a welcome object. what_to_say parameter is passed as "I love Python Tutorial"
x = Welcome("I love Python Tutorial")
x.say()

#using the set_sentence() method to change the sentence
x.set_sentence("Hello, World!")
x.say()
INFO