Python内部类

一 Python中内部类

典型定义:

class MyOuter:
    age=18
    def __init__(self,name):
        self.name=name

    class MyInner:
        def __init__(self,inner_name):
            self.inner_name=inner_name

out=MyOuter('lqz')
inner=out.MyInner('lqz_inner')
print(inner.inner_name)

二 内部类调用外部类的属性和方法

(注意是类属性和类方法,不是对象属性和对象的绑定方法)

class MyOuter:
    age=18
    def __init__(self,name):
        self.name=name
    @classmethod
    def outer_class_method(cls):
        print('我是外部类的类方法')

    class MyInner:
        def __init__(self,inner_name):
            self.inner_name=inner_name
        def inner_method(self):
            print('我是内部类的对象方法')
            MyOuter.outer_class_method()

out=MyOuter('lqz')
inner=out.MyInner('lqz_inner')
inner.inner_method()

三 内部类调用外部对象的对象属性和方法

需要在内部类构造的时候,把对象传过来

class MyOuter:
    age=18
    def __init__(self,name):
        self.name=name
    @classmethod
    def outer_class_method(cls):
        print('我是外部类的类方法')
    def outer_obj_method(self):
        print('我是外部类对象的绑定方法')

    class MyInner:
        def __init__(self,inner_name,obj):
            self.inner_name=inner_name
            self.obj=obj
        def inner_method(self):
            print('我是内部类的对象方法')
            MyOuter.outer_class_method()
            self.obj.outer_obj_method()

out=MyOuter('lqz')
inner=out.MyInner('lqz_inner',out)
inner.inner_method()
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from types import MethodType,FunctionType
def check(arg):
    if isinstance(arg,MethodType):
        return ('方法')
    if isinstance(arg,FunctionType):
        return ('函数')
    if callable(arg):
        return ('111')


class Foo(object):
    def f1(self):
        print('f1')

    @classmethod
    def f2(self):
        print('f2')

    @staticmethod
    def f3():
        print('f3')

    list_display = [f1, f2, f3]
    """
    <function Foo.f1 at 0x000000000268C040>
    <classmethod object at 0x00000000025CEE50>
    <staticmethod object at 0x00000000025CEFA0>
    """


obj = Foo()
Foo.list_display.append(obj.f1)
Foo.list_display.append(obj.f2)
Foo.list_display.append(obj.f3)
"""
<bound method Foo.f1 of <__main__.Foo object at 0x00000000025FD2E0>>
<bound method Foo.f2 of <class '__main__.Foo'>>
<function Foo.f3 at 0x000000000265B160>
"""

Foo.list_display.append(Foo.f1)
Foo.list_display.append(Foo.f2)
Foo.list_display.append(Foo.f3)

"""
<function Foo.f1 at 0x000000000265B040>
<bound method Foo.f2 of <class '__main__.Foo'>>
<function Foo.f3 at 0x000000000265B160>
"""

import traceback

for item in Foo.list_display:
    print(item,check(item),type(item))
    
"""
函数 <class 'function'>
None <class 'classmethod'>
None <class 'staticmethod'>
方法 <class 'method'>
方法 <class 'method'>
函数 <class 'function'>
函数 <class 'function'>
方法 <class 'method'>
函数 <class 'function'>


## 方法和函数
对象.xxx      xxx就是方法
类.xxx        xxx就是函数
xxx          xxx就是函数
"""


####
import traceback


def func():
    try:
        a += 1
    except Exception as e:
        # 获取当前错误的堆栈信息
        msg = traceback.format_exc()
        print(msg)
        # logging.error(str(e))
        # logging.error(str(msg))


func()
原文地址:https://www.cnblogs.com/bubu99/p/14008801.html