1. 程式人生 > >python基礎-物件_類反射、模組反射

python基礎-物件_類反射、模組反射

python面向物件中的反射:通過字串的形式操作物件相關的屬性。python中的一切事物都是物件(都可以使用反射)

getattr

物件獲取


class Manager:
    role = "管理員"
    def __init__(self,name,sex,phone,mail):
        self.name = name
        self.sex = sex
        self.phone = phone
        self.mail = mail

    def createClass(self):
        print("create class"
) def createTeacher(self): print("createTeacher") def createStu(): print("createStu") manager = Manager("safly","男",123456,123456) print("---物件獲取物件方法-----") f = getattr(manager,"createClass") f() print("---物件獲取物件屬性-----") name = getattr(manager,"name") print(name) print("---物件獲取類屬性-----"
) role = getattr(manager,"role") print(role) print("---物件獲取類屬性(可以設定預設值)-----") import logging if hasattr(manager,"role1"): role = getattr(manager,"role1","roleDefault") print(role) else: logging.warn("沒有role屬性") role = getattr(manager, "role1", "roleDefault") print(role)

輸出如下:

E:\python\python_sdk\python.exe E:/python/py_pro/demo.py
---物件獲取物件方法-----
create class
---物件獲取物件屬性-----
safly
---物件獲取類屬性-----
管理員
---物件獲取類屬性(可以設定預設值)-----
WARNING:root:沒有role屬性
roleDefault

類獲取


class Manager:
    role = "管理員"
    def createClass(self):
        print("create class")

    def createStu():
        print("createStu")

m = Manager()

f = getattr(Manager,"createClass")
f(Manager)

f = getattr(Manager,"createClass")
f(m)

role = getattr(Manager,"createStu")
role()

#物件獲取類屬性
role = getattr(Manager,"role")
print(role)

輸出如下:

create class
create class
createStu
管理員

setattr

設定類屬性、方法

class Manager:
    role = "管理員"
    def __init__(self,name,sex,phone,mail):
        self.name = name
        self.sex = sex
        self.phone = phone
        self.mail = mail

    def createClass(self):
        print("create class")

    def createTeacher(self):
        print("createTeacher")

    def createStu():
        print("createStu")


manager = Manager("safly","男",123456,123456)

print("----設定類屬性------")
setattr(Manager,"country","china")
print(Manager.country)

print("----刪除類屬性------")
delattr(Manager,"country")
# #刪除報錯
# print(Manager.country)
print("----設定類方法------")
def Method(parm):
    print("我是被繫結的class之外的方法parm--",parm)

setattr(Manager,"Method",Method)
Manager.Method("saf")
Manager.Method(Manager)
Manager.Method(manager)

def Method():
    print("我是被繫結的class之外的方法parm--")
setattr(Manager,"Method",Method)
Manager.Method()

輸出如下:

E:\python\python_sdk\python.exe E:/python/py_pro/demo.py
----設定類屬性------
china
----刪除類屬性------
----設定類方法------
我是被繫結的class之外的方法parm-- saf
我是被繫結的class之外的方法parm-- <class '__main__.Manager'>
我是被繫結的class之外的方法parm-- <__main__.Manager object at 0x0343B4B0>
我是被繫結的class之外的方法parm--

Process finished with exit code 0

設定物件屬性、方法


class Manager:
    role = "管理員"
    def __init__(self,name,sex,phone,mail):
        self.name = name
        self.sex = sex
        self.phone = phone
        self.mail = mail

    def createClass(self):
        print("create class")

    def createTeacher(self):
        print("createTeacher")

    def createStu():
        print("createStu")


manager = Manager("safly","男",123456,123456)

print("----設定物件屬性------")
setattr(manager,"age",20)
print(manager.age)

print("----刪除物件屬性------")
delattr(manager,"age")
# 'Manager' object has no attribute 'age'
# print(manager.age)

print("---物件不能刪除類屬性---")
setattr(Manager,"country","china")
print(Manager.country)
# delattr(manager,"country")
# print(Manager.country)

print("----設定物件方法------")
def create_course(self):
    print('建立了一個課程')

setattr(manager,'create_course',create_course)
manager.create_course(manager)

def create_grade():
    print('建立了一個班級')
setattr(manager,'create_grade',create_grade)
manager.create_grade()

輸出如下:

E:\python\python_sdk\python.exe E:/python/py_pro/demo.py
----設定物件屬性------
20
----刪除物件屬性------
---物件不能刪除類屬性---
china
----設定物件方法------
建立了一個課程
建立了一個班級

Process finished with exit code 0

模組反射

建立一個模組mokuai.py

a = 1
def method(rag):
    print(rag)
    return "666"

然後在python.py中匯入以上模組

import mokuai
print(getattr(mokuai,"a"))
method = getattr(mokuai,"method")
ret = method(8888)
print(ret)

輸出如下:

1
8888
666

反射本模組函式、變數

aa = 11
def method():
    print("---method---")
import sys

print(sys.modules[__name__])
print(getattr(sys.modules[__name__],"aa"))
f = getattr(sys.modules[__name__],"method")
f()

輸出如下:

E:\python\python_sdk\python.exe E:/python/py_pro/demo.py
<module '__main__' from 'E:/python/py_pro/demo.py'>
11
---method---

Process finished with exit code 0