• Day19:hasattribute;getattribute;seattributet;delattribute


    1:OS REVIEW:

     1 import sys,os
     2 
     3 # print(sys.argv)   #[a.py,post] can judge post or get
     4 # li = sys.argv
     5 #
     6 # if li[1] == 'post':
     7 #     print('post')
     8 # elif li[1] == 'down':
     9 #     print('1111')
    10 
    11 #the result:
    12 # E:Python_Projects>cd day27
    13 #
    14 # E:Python_ProjectsDay27>python os_review post
    15 # python: can't open file 'os_review': [Errno 2] No such file or directory
    16 #
    17 # E:Python_ProjectsDay27>python os_review.py post
    18 # post
    19 #
    20 # E:Python_ProjectsDay27>python os_review.py down
    21 # 1111
    22 
    23 # print(sys.path)
    24 # for i in sys.path:
    25 #     print(i)
    26 # print(sys.version)
    27 ##################################################
    28 # print(os.getcwd())
    29 # os.chdir(r'E:Python_ProjectsDay27')
    30 # os.makedirs('dir1dir2dir3')
    31 # os.removedirs('dir1dir2dir3')
    32 # print(os.listdir(os.getcwd()))
    33 # print(os.stat('os_review.py').st_size) #after the '.' include many functions
    34 # os.rename('dir1dir2dir3',r'dir1dir2aaa')
    35 # os.system('dir')
    36 # print(os.environ)
    37 # print(__file__)   #get the current path's filename
    38 # print(os.path.abspath(__file__))
    39 # print(os.path.split(os.path.abspath(__file__)))
    40 #('E:\Python_Projects\Day27', 'os_review.py')
    41 # print(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    42 # print(os.path.join(r'd:\','www','baidu','a.py'))
    43 print(os.path.join(os.path.dirname(os.path.abspath(__file__)),'dir1','dir2','123.txt'))
    View Code

    2:REFLECTION:

     1 #>>>>>>>>>>>>>>>>>>>>reflection of files:
     2 #test.py:
     3 # def say_hi():
     4 #     print('hello')
     5 
     6 # import test as obj
     7 # print(obj)
     8 # print(hasattr(obj,'say_hi'))
     9 # print(hasattr(obj,'say_hiiiiiiiii'))
    10 #
    11 # if hasattr(obj,'say_hi'):
    12 #     func=getattr(obj,'say_hi')
    13 #     func()
    14 # else:
    15 #     print('other logical')
    16 
    17 #>>>>>>>>>>>>>>>>>>>>judge the content in current file:
    18 x=111
    19 y=222
    20 import sys
    21 obj1 = sys.modules[__name__]
    22 print(hasattr(obj1,'x'))
    View Code

    3:GETATTIBUTE ERROR:

     1 class Foo:
     2     def __init__(self, x):
     3         self.x = x
     4 
     5     def __getattr__(self, item):
     6         print('executing __getattr__')
     7 
     8     def __getattribute__(self, item):
     9         print('executing __getattribute__')
    10         raise AttributeError('abnormal')   #go to __getattr__:avoid the collapse of the program
    11         # raise TabError('xxxxxx')
    12 
    13 f1 = Foo(10)
    14 print(f1.x)       #excuting __getattribute__
    15 # f1.xxxx         #excuting __getattribute__
    View Code

    4:CHANGE THE DISPLAY OF THE OBJECT:

     1 # l = list('hello')
     2 # print(l)
     3 # #['h', 'e', 'l', 'l', 'o']
     4 #
     5 # file = open('test.txt','w')
     6 # print(file)
     7 # <_io.TextIOWrapper name='test.txt' mode='w' encoding='cp936'>
     8 
     9 class Foo:
    10     def __init__(self,name,age):
    11         self.name = name
    12         self.age = age
    13 
    14     # def __str__(self):
    15     #     #   return 'made your own diaplay of the object '
    16     #     return 'the name is %s,age is %s'%(self.name,self.age)
    17 
    18     def __repr__(self):
    19         return 'the name is %s,age is %s'%(self.name,self.age)
    20 
    21 
    22 # f1 = Foo()
    23 f1 = Foo('alex',18)
    24 print(f1)  #str(f1)---->f1.__str__(),if no str will find the __repr__()
    25 # <__main__.Foo object at 0x0000000002476B20>
    26 # when use the __str__:the name is alex,age is 18
    View Code

    5:SETITEM;GETITEM AND DELITEM:

     1 #>>>>>>>>>>the three method is just used in dict:use '[]'
     2 
     3 class Foo:
     4     def __getitem__(self, item):
     5         print('getitem')
     6         return self.__dict__[item]
     7 
     8     def __setitem__(self, key, value):
     9         print('setitem')
    10         self.__dict__[key] = value
    11 
    12     def __delitem__(self, key):
    13         print('delitem')
    14         self.__dict__.pop(key)
    15 f1=Foo()
    16 print(f1.__dict__)
    17 f1['name']='zxver'
    18 f1['age']='18'
    19 print(f1.__dict__)
    20 # {}
    21 # setitem
    22 # setitem
    23 # {}
    24 # del f1.name    #did't trigure delitem
    25 # print(f1.__dict__)
    26 print(f1.age)    #did't trigure setitem
    27 del f1['name']   #trigure delitem
    28 print(f1.__dict__)
    29 print(f1['age'])
    View Code

    6:THE FORMAT OF THE TIME DISPALYING:

     1 # x= '{0}{0}{0}'.format('dog')
     2 # print(x)
     3 
     4 # '{0.year} {0.mon} {0.day}'
     5 # '{0.year}:{0.mon}:{0.day}'
     6 # '{0.year}-{0.mon}-{0.day}'
     7 format_dic ={
     8 
     9     'ymd':'{0.year} {0.mon} {0.day}',
    10     'y:m:d':'{0.year}:{0.mon}:{0.day}',
    11     'y-m-d':'{0.year}-{0.mon}-{0.day}',
    12 
    13 }
    14 class Date:
    15     def __init__(self,year,mon,day):
    16         self.year = year
    17         self.mon = mon
    18         self.day = day
    19 
    20     def __format__(self, format_spec):
    21         # print('i am execute')
    22         # print('--->',format_spec)
    23         if not format_spec or format_spec not in format_dic:
    24             format_spec = 'ymd'
    25         fm = format_dic[format_spec]
    26         return fm.format(self)
    27 
    28 d1 = Date(2020,4,22)
    29 # format(d1)
    30 print(format(d1,'y-m-d'))
    31 print(format(d1,''))
    32 print(format(d1,'fgsfhf'))
    33 # x = '{0.year} {0.mon} {0.day}'.format(d1)
    34 # x = '{0.year}:{0.mon}:{0.day}'.format(d1)
    35 # x = '{0.year}-{0.mon}-{0.day}'.format(d1)
    36 # print(x)
    View Code

    7:THE INDEX OF THE FILE:

    1 from dir1.dir2.a import C
    2 
    3 c1 = C('alex')
    4 print(c1.name)
    5 print(c1.__module__)    #dir1.dir2.a
    6 print(c1.__class__)     #<class 'dir1.dir2.a.C'>
    View Code

    8:IS INSTANCE OR IS SUBCLASS:

     1 class Foo:
     2     pass
     3 
     4 f1 = Foo()
     5 print(isinstance(f1,Foo))
     6 
     7 class Bar(Foo):
     8     pass
     9 # print(issubclass(Bar,Foo))
    10 b1 = Bar()
    11 print(isinstance(b1,Foo))
    12 print(type(b1))  #<class '__main__.Bar'>
    View Code

    9:ITERABLE FILE:

     1 #>>>>>>>>>>>>>>>>>>>>>>>1:provide a __next__method,use the next or report stopiteration
     2 #>>>>>>>>>>>>>>>>>>>>>>>2:use  the __iter__method to iterize the object
     3 
     4 class Foo:
     5     def __init__(self,n):
     6         self.n = n
     7     def __iter__(self):
     8         return self
     9     def __next__(self):
    10         if self.n >= 12:
    11             raise StopIteration('stop iteration')
    12         self.n += 1
    13         return self.n
    14 
    15 # l=list('hello')
    16 # for i in l:
    17 #     print(i)
    18 
    19 f1 = Foo(10)
    20 print(f1.__next__())
    21 print(f1.__next__())
    22 print(f1.__next__())
    23 # print(f1.__next__())
    24 # print(next(f1))
    25 # print(next(f1))
    26 # print(next(f1))
    27 
    28 # for i in f1:  #f1.__iter__() == iter(f1)
    29 #     print(i)
    View Code

    10:SLOTS:#  #use __slot__to replace __dict__ to  save memory of objects:but use it after conider

     1 # class Foo:
     2 #     __slots__=['name','age'] #{'name':None,'age':None}
     3 #     # __slots__ = 'name'
     4 #
     5 # f1=Foo()
     6 # f1.name = 'alex'
     7 # print(f1.name)
     8 # # print(f1.__dict__)  #>>>AttributeError: 'Foo' object has no attribute '__dict__'
     9 # print(f1.__slots__)
    10 # f1.age = 17
    11 # print(f1.name)
    12 # print(f1.age)
    13 # # print(f1.gender)  #wrong
    14 #
    15 # f2=Foo()
    16 # print(f2.__slots__)
    17 # f2.name='liu'
    18 # f2.age=15
    19 # print(f2.name)
    20 # print(f2.age)
    21 # #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>doc:It can't be herited!
    22 # class Foo:
    23 #     'hello world!'
    24 #     pass
    25 # class Bar(Foo):
    26 #     pass
    27 # print(Bar.__doc__)
    28 #
    29 # #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>module and class:
    30 # #a.py:
    31 # # class C:
    32 # #     def __init__(self,name):
    33 # #         self.name = 'SB'
    34 #
    35 # # from dir1.dir2.a import C
    36 # #
    37 # # c1 = C('alex')
    38 # # print(c1.name)
    39 # # print(c1.__module__)    #dir1.dir2.a
    40 # # print(c1.__class__)     #<class 'dir1.dir2.a.C'>
    41 #
    42 # #>>>>>>>>>>>>>>>>>>>>>>>>__del__:just trigure when delete the class or when the program stop to excute will trigure it
    43 # class Foo:
    44 #     def __init__(self,name):
    45 #         self.name = name
    46 #     def __del__(self):
    47 #         print('I executing')
    48 #
    49 # f1 = Foo('alex')
    50 # # del f1.name  #>>>>>>'---------->'first
    51 # # del f1         #>>>>>>'I executing'first
    52 # print('-------------->')     ##>>>>>>'---------->'first,the same with del f1.name
    53 # print('-------------->')     ##>>>>>>'---------->'first,the same with del f1.name
    54 
    55 #>>>>>>>>>>>>>>>>>>>>>>>>__call__:
    56 class Foo:
    57     # def __init__(self,name):
    58     #     self.name = name
    59     def __call__(self):
    60         print('I executing')
    61 
    62 f1 = Foo()
    63 f1()   #Foo()'s __call__method
    View Code

    11:斐波那契数列:

     1 class Fib:
     2     def __init__(self):
     3         self._a = 1
     4         self._b = 1
     5 
     6     def __iter__(self):
     7         return self
     8 
     9     def __next__(self):
    10         if self._a >100:
    11             raise StopIteration('stopiteration')
    12         self._a,self._b = self._b,self._a + self._b
    13         return self._a
    14 
    15 f1 = Fib()
    16 print(next(f1))
    17 print(next(f1))
    18 print(next(f1))
    19 print(next(f1))
    20 print('===========================')
    21 for i in f1:
    22     print(i)
    View Code

    12:THE PRIORITY:

    #>>>>>>>>>>>>>>>>>>>>the priority :1,class;2,data descriptor;3,instance;
    #>>>>>>>>>>>>>>>>>>>>4,nondata descripter == 5,trigure __getattr__()when can't find the attribute
  • 相关阅读:
    httpd设置HTTPS双向认证
    crossdomain.xml的配置详解
    hibernate中的merge()方法
    解决java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver问题
    oracle自定义函数:将使用点分隔符的编码转成层级码格式的编码
    jsp页面科学计数法显示问题的解决办法
    javascript检索某个字符或字符串在源字符串中的位置(下标)
    webwork遍历数组标签
    过多得操作DOM会降低WEB应用的性能
    vue中$refs的用法及作用详解
  • 原文地址:https://www.cnblogs.com/zxver/p/12794290.html
Copyright © 2020-2023  润新知