Created
December 22, 2017 19:03
-
-
Save anilpai/b3cc383e9957fe7f6c23c4cf4b31a440 to your computer and use it in GitHub Desktop.
Python @classmethod and @staticmethod
This file contains hidden or 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
class Student: | |
def __init__(self, first_name, last_name): | |
self.first_name = first_name | |
self.last_name = last_name | |
@classmethod | |
def from_string(cls, name_str): | |
first_name, last_name = map(str, name_str.split(' ')) | |
student = cls(first_name, last_name) | |
return student | |
@classmethod | |
def from_json(cls, name_str): | |
# parse the json | |
return name_str | |
@staticmethod | |
def is_full_name(name_str): | |
names = name_str.split(" ") | |
return len(names) > 1 | |
if __name__=='__main__': | |
stud = Student('Anil', 'Pai') | |
print(stud.first_name, stud.last_name) | |
stud = Student.from_string('Anil Pai') | |
print(stud.first_name, stud.last_name) | |
flag = Student.is_full_name('Anil Pai') | |
print(flag) | |
flag = Student.is_full_name('Anil') | |
print(flag) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment