Created
November 2, 2019 05:11
-
-
Save leimao/15d0908b3d83463d9fee1916a4e1c270 to your computer and use it in GitHub Desktop.
Python Attributes
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
import module | |
class Chinese(object): | |
# Class attributes | |
nationality = "China" | |
# Instance attributes | |
def __init__(self, name, birth_year): | |
self.name = name | |
self.birth_year = birth_year | |
def get_name(self): | |
return self.name | |
def get_age(self, year): | |
return year - self.birth_year | |
@staticmethod | |
def get_province(cls, city): | |
if city == "Qingdao": | |
return "Shandong" | |
else: | |
return "Other than Shandong" | |
def get_fake_name(): | |
return "Jingze Wang" | |
fake_nationality = "American" | |
attr_0 = "nationality" | |
attr_1 = "name" | |
attr_2 = "get_name" | |
attr_3 = "get_province" | |
print("Checking Class attributes ...") | |
print("Has attribute {}: ".format(attr_0)) | |
print(hasattr(Chinese, attr_0)) | |
print("Has attribute {}: ".format(attr_1)) | |
print(hasattr(Chinese, attr_1)) | |
print("Has attribute {}: ".format(attr_2)) | |
print(hasattr(Chinese, attr_2)) | |
print("Has attribute {}: ".format(attr_3)) | |
print(hasattr(Chinese, attr_3)) | |
print("Getting nationality: ") | |
print(getattr(Chinese, attr_0)) | |
print("Getting fake nationality: ") | |
# Overwrites attributes | |
setattr(Chinese, attr_0, fake_nationality) | |
print(getattr(Chinese, attr_0)) | |
print("-"*50) | |
person_0 = Chinese(name="Xiaolong Li", birth_year=1990) | |
print("Checking Instance attributes ...") | |
print("Has attribute {}: ".format(attr_0)) | |
print(hasattr(person_0, attr_0)) | |
print("Has attribute {}: ".format(attr_1)) | |
print(hasattr(person_0, attr_1)) | |
print("Has attribute {}: ".format(attr_2)) | |
print(hasattr(person_0, attr_2)) | |
print("Has attribute {}: ".format(attr_3)) | |
print(hasattr(person_0, attr_3)) | |
print("Getting Instance attributes ...") | |
print("Getting name: ") | |
print(person_0.get_name()) | |
print("Getting fake name {}: ".format(attr_1)) | |
# Overwrites attributes | |
setattr(person_0, attr_2, get_fake_name) | |
print(person_0.get_name()) | |
print("Getting nationality: ") | |
print(getattr(person_0, attr_0)) | |
print("-"*50) | |
attr_4 = "get_fake_name" | |
attr_5 = "get_fake_age" | |
attr_6 = "get_age" | |
print("Checking Module attributes ...") | |
print("Has attribute {}: ".format(attr_4)) | |
print(hasattr(module, attr_4)) | |
print("Has attribute {}: ".format(attr_5)) | |
print(hasattr(module, attr_5)) | |
print("Getting age: ") | |
print(person_0.get_age(2019)) | |
print("Getting fake age: ") | |
# Overwrites attributes | |
setattr(person_0, attr_6, module.get_fake_age) | |
print(person_0.get_age(2019)) |
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
def get_fake_age(*args, **kwargs): | |
return 18 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment