>

Records项目学习,python基础教程

- 编辑:正版管家婆马报彩图 -

Records项目学习,python基础教程

Python,python基础教程

Records学习笔记

                                      Records链接地址

1. __getitem__(self, key) 内建格局(Build-in)

例子:

 1 class Test(object):
 2 
 3     def __getitem__(self, key):
 4 
 5         print("test")
 6 
 7         return 1
 8 
 9 
10 
11 t = Test()
12 
13 print(t["m"])

 

运维结果:

>>> hello
>>> 1

  

 

2. ___getattr__(self, name) 内建方式(Build-in)

该措施在未找到对象的属性值的时候调用.

例子:

1 class Test(object):
2     def __getattr__(self, name):
3         print("test")
4         return 1
5 
6 t = Test()
7 print(t.m)

运作结果:

>>> hello
>>> 1

 

去掉__getattr__函数时,会提醒未有m那特性情的谬误。

 

3. @property 装饰器

用来将一个函数生产对应的getter函数

实例:

class Test(object):
    @property
    def name(self):
        return self._name
    @name.setter
    def name(self, name):
        self._name = name

t = Test()
t.name = "hello"
print(t.name)

运营结果:

>>> hello

 

4. A if 条件 else B(语法)

print("1") if __name__=='__main__' else print("2")

 

5. __enter__和__exit__内建措施(Build-in)

用来with语句块,当步入with语句块的时候调用__enter__

当离开with语句块的时候调用__exit__

例子:

 1 class test(object):
 2     def __enter__(self):
 3         print("enter")
 4         return self
 5     def __exit__(self, type, value, traceback):
 6         print("out")
 7         del self
 8 with test() as m:
 9     m.name = 1
10     print (m.name)

运作结果:

>>> enter
>>> 1
>>> out

 

  1.  __iter__(self) 迭代器(Build-in)

通过__iter__函数获得四个迭代器对象, 调用迭代器的next方法不断赢得下四个值.

例子:

 1 class test(object):
 2     def __init__(self):
 3         self.a = 1
 4         self.b = 1
 5     def __iter__(self):
 6         return self
 7     def __next__(self):
 8         test = self.a + self.b
 9         self.a, self.b = self.b, test
10         return self.a
11     def next(self):
12         return self.__next__
13 
14 for i in test():
15     if i > 1000:
16         break
17     print(i)

 

Records学习笔记 Records链接地址 1.__getitem__(self, key) 内建艺术(Build-in) 例子: 1 class Test(object): 2 3 def __getitem__ (self, key): 4...

Records学习笔记

                                      Records链接地址

1. __getitem__(self, key) 内建格局(Build-in)

例子:

 1 class Test(object):
 2 
 3     def __getitem__(self, key):
 4 
 5         print("test")
 6 
 7         return 1
 8 
 9 
10 
11 t = Test()
12 
13 print(t["m"])

 

运维结果:

>>> hello
>>> 1

  

 

2. ___getattr__(self, name) 内建方式(Build-in)

该办法在未找到对象的属性值的时候调用.

例子:

1 class Test(object):
2     def __getattr__(self, name):
3         print("test")
4         return 1
5 
6 t = Test()
7 print(t.m)

运作结果:

>>> hello
>>> 1

 

去掉__getattr__函数时,会唤起未有m这特性子的失实。

 

3. @property 装饰器

用来将二个函数生产对应的getter函数

实例:

class Test(object):
    @property
    def name(self):
        return self._name
    @name.setter
    def name(self, name):
        self._name = name

t = Test()
t.name = "hello"
print(t.name)

运营结果:

>>> hello

 

4. A if 条件 else B(语法)

print("1") if __name__=='__main__' else print("2")

 

5. __enter__和__exit__内建办法(Build-in)

用来with语句块,当步入with语句块的时候调用__enter__

当离开with语句块的时候调用__exit__

例子:

 1 class test(object):
 2     def __enter__(self):
 3         print("enter")
 4         return self
 5     def __exit__(self, type, value, traceback):
 6         print("out")
 7         del self
 8 with test() as m:
 9     m.name = 1
10     print (m.name)

运作结果:

>>> enter
>>> 1
>>> out

 

  1.  __iter__(self) 迭代器(Build-in)

通过__iter__函数获得二个迭代器对象, 调用迭代器的next方法不断得到下二个值.

例子:

 1 class test(object):
 2     def __init__(self):
 3         self.a = 1
 4         self.b = 1
 5     def __iter__(self):
 6         return self
 7     def __next__(self):
 8         test = self.a + self.b
 9         self.a, self.b = self.b, test
10         return self.a
11     def next(self):
12         return self.__next__
13 
14 for i in test():
15     if i > 1000:
16         break
17     print(i)

 

本文由编程应用发布,转载请注明来源:Records项目学习,python基础教程