类的反射与内置方法

# class Foo:
#     f = '类的静态变量'
#     def __init__(self,name,age):
#         self.name=name
#         self.age=age
#
#     def say_hi(self):
#         print('hi,%s'%self.name)
#
# obj=Foo('egon',73)
#
# #检测是否含有某属性
# print(hasattr(obj,'name'))
# print(hasattr(obj,'say_hi'))
#
# #获取属性
# n=getattr(obj,'name')
# print(n)
# func=getattr(obj,'say_hi')
# func()
# #
# print(getattr(obj,'aaaaaaaa','不存在啊')) #报错
# #
# # #设置属性
# setattr(obj,'sb',True)
# setattr(obj,'show_name',lambda self:self.name+'sb')
# print(obj.__dict__)
# print(obj.show_name(obj))
#
# #删除属性
# delattr(obj,'age')
# delattr(obj,'show_name')
# delattr(obj,'show_name111')#不存在,则报错
#
# print(obj.__dict__)
#
# 对实例化对象的示例
# import time
# class A:
#     def __init__(self):
#         self.a = 1
#         self.b = 2
#
#     def __len__(self):
#         return len(self.__dict__)
# a = A()
# time.sleep(5)
# print(len(a))
import time


# def func1():
#
#     print('in func1')
#
#
# start = time.time()
# func1()
# print(time.time() - start)
#     •    现有两元组(('a'), ('b')), (('c'), ('d')), 请使用python中匿名函数生成列表[{'a': 'c'}, {'b': 'd'}]
# s = (('a'), ('b'))
# s1 = (('c'), ('d'))
# lst = list(map(lambda x, y:{x:y}, s, s1))
# print(lst)
# for i in range(1, 10):
#     for j in range(1, i + 1):
#         # print('%s * %s = %s' % (j, i, i * j), end='\t')
#         print('{} * {} = {}'.format(i, j, i * j), end=' \t ')
#     print()
# class A:
#     NAME="张"
#     def __init__(self):
#         self.s="abdnakhhff"
#     @classmethod
#     def eat(cls):
#         print("吃啥?")
#     @staticmethod
#     def watch():
#         print("你瞅啥?")
#     def __len__(self):
#         return len(self.s)
# p=A()
# print(len(p))
# num=input("请输入:")
# if hasattr(A,num):
#    getattr(A,num)()
# class A:pass
# class B(A):
#     pass
# b=B()
# print(isinstance(b,B))
# print(isinstance(b,A))
# print(issubclass(B,A))

# class A:
#     GAME="LOL"
#     def __init__(self,name):
#         self.name=name
#     @classmethod
#     def eat(cls):
#         return cls.GAME
#     @staticmethod
#     def watch():
#         print("登录")
# a=A("li")
# print(hasattr(a,"a"))
# print(getattr(a,"name"))
# setattr(a,"name","zhang")
# print(a.name)
# delattr(a,"name")
# print(a.__dict__)
# lst=[1,2,3,4,5,]
# for index,i in enumerate(lst,2):
#     print(index,i)
# import sys
# file=sys.modules["__main__"]
# cls=getattr(file,"A")
# cls.watch()
# __call__用法:
# class A:
#     def __call__(self, *args, **kwargs):
#         print(666)
#
#
# class B:
#     def __init__(self, cls):
#         self.a = A()
#         self.a()
#
#
# a = A()
# a()
# B(A)
# class A:
#     def __init__(self,a):
#         self.a=a
#     def __len__(self):
#         return len(self.a)
# a=A("aaaaaaa")
# print(len(a))#len(对象)执行__len__函数,与对象无关
# 面试题:********单例类
# class A:
#     __OBJECT = None
#
#     def __new__(cls, *args, **kwargs):
#         if not cls.__OBJECT:
#             cls.__OBJECT = object.__new__(cls)
#         return cls.__OBJECT
#
#     def __init__(self, name):
#         self.name = name
#         print(666)
#
#
# a = A("zhang")
# b = A("wang")
# print(a.name,b.name)
# class A:
#     def __str__(self):
#        return"%s"%self.name
#     def __init__(self,name):
#         self.name=name
# a=A("zhang")
# print(str(a))
# print(a)
# print("%s在家"%a)

猜你喜欢

转载自www.cnblogs.com/zhangdaye/p/9398147.html