class List(list):
def append(self, p_object):
if type(p_object) is str:
# self.append(p_object)
super().append(p_object)
else:
print('只能添加字符串类型')
def show_midlle(self):
mid_index=int(len(self)/2)
return self[mid_index]
# l2=list('hell oworld')
# print(l2,type(l2))
l1=List('helloworld')
# print(l1,type(l1))
# print(l1.show_midlle())
l1.append(1111111111111111111111)
l1.append('SB')
print(l1)
module_t=__import__('m1.t')
print(module_t)
# module_t.t.test1()
# from m1.t import *
# from m1.t import test1,_test2
#
# test1()
# _test2()
import importlib
m=importlib.import_module('m1.t')
print(m)
m.test1()
m._test2()
#_*_coding:utf-8_*_
__author__ = 'Linhaifeng'
class H2O:
def __init__(self,name,temperature):
self.name=name
self.temperature=temperature
def turn_ice(self):
if self.temperature < 0:
print('[%s]温度太低结冰了' %self.name)
elif self.temperature > 0 and self.temperature < 100:
print('[%s]液化成水' %self.name)
elif self.temperature > 100:
print('[%s]温度太高变成了水蒸气' %self.name)
def aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(self):
pass
class Water(H2O):
pass
class Ice(H2O):
pass
class Steam(H2O):
pass
w1=Water('水',25)
i1=Ice('冰',-20)
s1=Steam('蒸汽',3000)
# w1.turn_ice()
# i1.turn_ice()
# s1.turn_ice()
def func(obj):
obj.turn_ice()
func(w1) #---->w1.turn_ice()
func(i1) #---->i1.turn_ice()
# def func(obj):
# obj.turn_ice()
#
# func(w1)
# func(i1)
# func(s1)
class BlackMedium:
feture='Ugly'
def __init__(self,name,addr):
self.name=name
self.addr=addr
def sell_hourse(self):
print('【%s】 正在卖房子,傻逼才买呢' %self.name)
def rent_hourse(self):
print('【%s】 正在租房子,傻逼才租呢' % self.name)
print(hasattr(BlackMedium,'feture'))
getattr()
#
# b1=BlackMedium('万成置地','天露园')
# b1.name--->b1.__dic__['name']
# print(b1.__dict__)
#
# # b1.name
# # b1.sell_hourse
# print(hasattr(b1,'name'))
# print(hasattr(b1,'sell_hourse'))
# print(hasattr(b1,'selasdfasdfsadfasdfasdfasdfasdl_hourse'))
#
#
#
# print(getattr(b1,'name'))
# print(getattr(b1,'rent_hourse'))
# func=getattr(b1,'rent_hourse')
# func()
# # print(getattr(b1,'rent_hourseasdfsa')) #没有则报错
# print(getattr(b1,'rent_hourseasdfsa','没有这个属性')) #没有则报错
#
#
# # b1.sb=True
# setattr(b1,'sb',True)
# setattr(b1,'sb1',123)
# setattr(b1,'name','SB')
# setattr(b1,'func',lambda x:x+1)
# setattr(b1,'func1',lambda self:self.name+'sb')
# print(b1.__dict__)
# print(b1.func)
# print(b1.func(10))
# print(b1.func1(b1))
# del b1.sb
# del b1.sb1
# delattr(b1,'sb')
# print(b1.__dict__)
import time
class FileHandle:
def __init__(self,filename,mode='r',encoding='utf-8'):
# self.filename=filename
self.file=open(filename,mode,encoding=encoding)
self.mode=mode
self.encoding=encoding
def write(self,line):
print('------------>',line)
t=time.strftime('%Y-%m-%d %X')
self.file.write('%s %s' %(t,line))
def __getattr__(self, item):
# print(item,type(item))
# self.file.read
return getattr(self.file,item)
f1=FileHandle('a.txt','w+')
# print(f1.file)
# print(f1.__dict__)
# print('==>',f1.read) #触发__getattr__
# print(f1.write)
f1.write('1111111111111111\n')
f1.write('cpu负载过高\n')
f1.write('内存剩余不足\n')
f1.write('硬盘剩余不足\n')
# f1.seek(0)
# print('--->',f1.read())
# class Foo:
# x=1
# def __init__(self,y):
# self.y=y
#
# def __getattr__(self, item):
# print('执行__getattr__')
#
# f1=Foo(10)
# print(f1.y)
# print(getattr(f1,'y')) #len(str)--->str.__len__()
# f1.sssssssssssssssssssssssssssssssssssss
# class Foo:
# x=1
# def __init__(self,y):
# self.y=y
#
# def __delattr__(self, item):
# print('删除操作__delattr__')
#
# f1=Foo(10)
# del f1.y
# del f1.x
#
# class Foo:
# x=1
# def __init__(self,y):
# self.y=y
#
# def __setattr__(self, key, value):
# print('__setattr__执行')
# # self.key=value
# self.__dict__[key]=value
# f1=Foo(10)
# print(f1.__dict__)
# f1.z=2
# print(f1.__dict__)
# class Foo:
# def __getattr__(self, item):
# print('------------->')
#
# # print(Foo.__dict__)
# print(dir(Foo))
# f1=Foo()
#
# print(f1.x) #只有在属性不存在时,会自动触发__getattr__
#
# del f1.x #删除属性时会触发_delattr__
#
# f1.y=10
# f1.x=3 # 设置属性的时候会触发——setattr———
class Foo:
def __init__(self,name):
self.name=name
def __getattr__(self, item):
print('你找的属性【%s】不存在' %item)
def __setattr__(self, k,v):
print('执行setattr',k,v)
if type(v) is str:
print('开始设置')
# self.k=v #触发__setattr__
self.__dict__[k]=v.upper()
else:
print('必须是字符串类型')
def __delattr__(self, item):
print('不允许删除属性【%s】' %item)
# print('执行delattr',item)
# del self.item
# self.__dict__.pop(item)
f1=Foo('alex')
# f1.age=18 #触发__setattr__
# print(f1.__dict__)
# print(f1.name)
# print(f1.age)
# print(f1.gender)
# print(f1.slary)
print(f1.__dict__)
del f1.name
print(f1.__dict__)