Created
May 19, 2020 17:45
-
-
Save ZhouYang1993/f38930cf56f1cdbfa9987607e0b12009 to your computer and use it in GitHub Desktop.
Class Method and Static Method in Python
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 get_from_string(cls, name_string: str): | |
| first_name, last_name = name_string.split() | |
| if Student.validate_name(first_name) and Student.validate_name(last_name): | |
| return cls(first_name, last_name) | |
| else: | |
| print('Invalid Names') | |
| @staticmethod | |
| def validate_name(name): | |
| return len(name) <= 10 | |
| Yang = Student.get_from_string('yang zhou') | |
| print(Yang.first_name) # yang | |
| print(Yang.last_name) # zhou |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment