Skip to content

Instantly share code, notes, and snippets.

@wayslog
Created March 9, 2017 06:57
Show Gist options
  • Save wayslog/34f976bed677945385d8d28ec03bbb79 to your computer and use it in GitHub Desktop.
Save wayslog/34f976bed677945385d8d28ec03bbb79 to your computer and use it in GitHub Desktop.
from collections import namedtuple
class Parent(namedtuple("Parent",'name')):
def show(self):
print(self.name)
class Child(namedtuple("FirstChild", "sex"), Parent):
def show_sex(self):
print(self.sex)
class RevExtendChild(Parent, namedtuple("RevExtendChild", "age")):
def show_age(self):
print(self.age)
parent = Parent(name="elton")
parent.show()
# 这里,父类里明明有 name ,在这里却不能赋值给他
child = Child(sex="男")
child.show_sex()
# 这里,将 sex 代替 name 打印了出来....
# output[12]: 男
child.show()
# 这里交换继承顺序之后更惨,赋值 age 给这里直接就不能用了
rev_child = RevExtendChild(age=12)
rev_child.show_age()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment