02-05 21—25

______egon新书python全套来袭请看:https://egonlin.com/book.html

21、类的加载顺序(类中有继承有构造有静态)?

1、 类对象 
2、 实例对象 
3、 self变量名称问题 
4、 类属性、实例变量、局部变量 
5、 类方法 
6、 实例方法 
7、 类方法与实例方法相互调用 
8、 静态方法 
9、 继承时三类方法的影响 

22、参考下面代码片段

class Context:
    pass

with Context as ctx:
    ctx.do_something()
    
# 请在 Context 类下添加代码完成该类的实现 

# 答案:
class Context(object):
    def __enter__(self):
        pass

    def __exit__(self, exc_type, exc_val, exc_tb):
        if all([exc_type, exc_val, exc_tb]):
            print 'handler except'
            print 'exception {}'.format(exc_val)
        return True

def main():
    with tornado.stack_context.StackContext(Contextor):
        async_task()

23、以下代码输出是什么?请给出答案并解释。



class Parent:
    x = 1

class Child1(Parent):
    pass

class Child2(Parent):
    pass
print(Parent.x, Child1.x, Child2.x)
Child1.x = 2
print(Parent.x, Child1.x, Child2.x)
Child1.x = 3
print(Parent.x, Child1.x, Child2.x)

# 答案
'''
1 1 1
1 2 1
1 3 1
'''

24、函数del_node(self,data)的功能:在根节点指针为root的二叉树(又称二叉 排序树)上排除数值为 K 的节点,若删除成功,返回 0,否则返回-1, 概述节点的 定义类型为

class Node(object):
    def __init__(self, data):
        self.data = data  # 节点的数值 
        self.left_child = Node  # 指向左右子树的指针 
        self.right_child = Node
        
    def set_data(self, data):
        self.data = data

# 答案: 

25、请给出下面代码片段的输出,请简述上面代码需要改进的地方?

class Singleton:
    _instance = None
    def __new__(cls, *args, **kwargs):
        print("New")
        if cls._instance is None:
            print("Create")
            cls._instance = super().__new__(cls,*args, **kwargs)
        return cls._instance
    def __init__(self):
        print("Initalize")
        self.prop = None

s1 = Singleton()
s2 = Singleton()

# 答案:

posted @ 2019-10-22 20:16  小猿取经-林海峰老师  阅读(947)  评论(1编辑  收藏  举报