Created
June 24, 2018 06:29
-
-
Save snakeneedy/394c498b1676d5f5f803af052484a593 to your computer and use it in GitHub Desktop.
Python 3: sample of class methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Result: | |
call self_method by Inst | |
--- | |
call self_method by Temp | |
--- | |
call class_method by Inst | |
call self_method by class_method | |
--- | |
call class_method by Temp | |
call self_method by class_method | |
--- | |
call static_method by Inst | |
--- | |
call static_method by Temp | |
""" | |
class Temp: | |
def self_method(self, msg): | |
print('call self_method by ' + msg) | |
@classmethod | |
def class_method(cls, msg): | |
print('call class_method by ' + msg) | |
cls.self_method(cls, 'class_method') | |
@staticmethod | |
def static_method(msg): | |
print('call static_method by ' + msg) | |
Inst = Temp() | |
Inst.self_method('Inst') | |
print('---') | |
Temp.self_method(Temp, 'Temp') | |
print('---') | |
Inst.class_method('Inst') | |
print('---') | |
Temp.class_method('Temp') | |
print('---') | |
Inst.static_method('Inst') | |
print('---') | |
Temp.static_method('Temp') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment