036-001. writing method in class
@
First parameter of method should be "self"
@
class Person:
def greeting(self):
print('Hello')
# I can create instance of Person class by appending parenthesis next to name of class
james = Person()
print(james)
# <__main__.Person object at 0x027E3AD0>
# This is "Person object" which is located at 0x027E3AD0 in memory
@
# I invoke greeting() of james instance
james.greeting()
# Hello
@
# I can create empty class by using "pass" keyword
class Person:
pass
@
class Person:
def greeting(self):
# I invoke print() which is located in outside of Person class
print('Hello')
def hello(self):
# I invoke greeting() which is located in Person class
# by using self.method() syntax
self.greeting()
# I invoke greeting() which is located in outside of Person class
greeting()
james = Person()
james.hello()
# output:
# Hello
@
# I use isinstance(instance, class) when I want to know if some instance comes from specific class or not
class Person:
pass
james = Person()
isinstance(james, Person)
# output:
# True