Python学习之路—面向对象进阶

isinstance和issubclass

isinstance(test_obj,Test):

# 判断一个对象是不是该类的对象,或其派生类的对象。

# 判断test_obj对象是否是Test类的对象。

1 class Test:
2     pass
3 
4 test_obj = Test()
5 ret = isinstance(test_obj,Test)
6 print(ret)

# 如果这个类有父类,那个这个对象也是其父类的对象。

1 class Test:
2     pass
3 
4 test_obj = Test()
5 ret = isinstance(test_obj,object)    # object是所有类的父类
6 print(ret)

小应用:

num = 10
print(isinstance(num, int))
print(type(num) is int)  # 判断数据类型是最好使用该方法

from collections import Iterable
li = [i for i in range(10)]
print(isinstance(li, Iterable))  # 判断一个对象是否是可迭代对象

 

issubclass(sub, super)

#判断sub类是否是 super 类的子类

class Subclass:
    pass

ret = issubclass(Subclass,object)
print(ret)

 

反射

反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问、检测和修改它本身状态或行为的一种能力(自省)。这一概念的提出很快引发了计算机科学领域关于应用反射性的研究。它首先被程序语言的设计领域所采用,并在Lisp和面向对象方面取得了成绩。

hasattr(*args, **kwargs):

# 1.  检测对象是否含有某属性或方法,结果返回True或False

class Test:
    role = 'teacher'
    def __init__(self,name,sex):
        self.name = name
        self.sex = sex

    def happy(self):
        print('%s is happy' %(self.name))
        return 'End'

alex = Test('alex','male')

ret = hasattr(alex,'sex')
print(ret)
ret2 = hasattr(alex,'age')
print(ret2)
ret3 = hasattr(alex,'happy')
print(ret3)

运行结果:
True
False
True

 

# 2. 检测模块中是否含有某属性或方法,结果返回True或False 

import my_module

ret = hasattr(my_module,'test')
print(ret)
ret2 = hasattr(my_module,'num')
print(ret2)

运行结果:
True
True

 

getattr(object, name, default=None)

# 1. 获取对象的属性或执行对象的方法。该属性或方法是字符串形式的。 

class Test:
    role = 'teacher'
    def __init__(self,name,sex):
        self.name = name
        self.sex = sex

    def happy(self,x):
        print('%s is happy' %(self.name))
        return x+'End'

alex = Test('alex','male')
ret1 = getattr(alex,'role')
print(ret1)
ret2 = getattr(alex,'sex')
print(ret2)
func = getattr(alex,'happy')
res = func('python')
print(res)

运行结果:
  teacher
  male
  alex is happy
  python End

# 2. 获取模块的属性和执行模块中的方法

import sys

num = 100

# sys.modules[__name__] 获取当前模块对象
print(sys.modules[__name__])

if hasattr(sys.modules[__name__], 'num'):
ret = getattr(sys.modules[__name__], 'num')
print(ret)

# 运行结果: 

  <module '__main__' from 'E:/Python_Code/面向对象进阶/hasattr.py'>
  100

hasatte和getattr是最佳搭档
  类名调属性或方法(静态(类)属性,静态方法和类方法)
  对象名调属性或方法(对象属性,普通方法(self))
  模块名调用属性或方法(变量,函数)
  在自己模块中调用属性或方法(变量,函数)
    自己的模块表示:sys.modules[__name__]

 

 hasattr和getattr的组合应用

 1 class Login():
 2     def login(self):
 3         print('in the login')
 4 
 5     def register(self):
 6         print('in the register')
 7 
 8 obj = Login()
 9 
10 func= input('Please enter func: ')
11 if hasattr(obj,func):
12     func = getattr(obj,func)
13     func()

 

__new__

class A:
    def __init__(self):
        self.x = 1
        print('in init function')
    def __new__(cls, *args, **kwargs):
        print('in new function')
        return object.__new__(A, *args, **kwargs)

a = A()
print(a.x)

单例模式

所谓单例,是指一个类的实例从始至终只能被创建一次。

 

posted @ 2017-09-24 17:35  流星之泪  阅读(139)  评论(0)    收藏  举报