Python 類的高階特性

2021-08-04 07:16:21 字數 3846 閱讀 9365

可以繫結變數和方法

如果需要繫結的方法來呼叫類內部的成員,該方法的第乙個引數為self

動態繫結的變數與方法只作用於該物件

>>> 

class

user_info

(object):

...

pass

...

>>>

deffoo

():... print('foo')

...

>>>

deffoo_class

(self):

... print(self.name)

...

>>> u = user_info()

>>> u.name = 'root'

>>> u.pwd = 'admin'

>>> u.foo = foo

>>> u.foo_class = foo_class

>>> u.name

'root'

>>> u.pwd

'admin'

>>> u.foo()

foo>>> u.foo_class()

traceback (most recent call last):

file "", line 1, in

typeerror: foo_class() missing 1 required positional argument: 'self'

靜態成員可以動態繫結, 也可以在類中直接宣告

靜態成員是屬於類的, 不屬於物件

>>> 

class

user_info

(object):

name = 'aaa'

>>> user_info.name

'aaa'

>>> a = user_info()

>>> a.name

'aaa'

>>> b = user_info()

>>> b.name

'aaa'

>>> a.name = 'bbb'

>>> a.name

'bbb'

>>> b.name

'aaa'

>>> user_info.name

'aaa'

>>> user_info.name = 'ccc'

>>> a.name

'bbb'

>>> b.name

'ccc'

>>>

在使用類的動態繫結的時候可能會出現誤操作.

比如在給物件賦值時寫錯了變數名,就會變成動態繫結

>>> 

class

user_info

(object):

name = 'aaa'

>>> a = user_info()

>>> a.nama = 'bbb'

#本意是給name賦值,結果繫結了乙個nama的變數

可以使用slots方法來進行限定

>>> class cls_demo(object):

... __slots__ = ('name', 'pwd') # 只允許繫結 name 和 pwd

...

>>> demo1 = cls_demo()

>>> demo1.user = 'root'

traceback (most recent call last):

file "", line 1, in

attributeerror: 'cls_demo' object has no attribute 'user'

>>> demo1.name = 'admin'

>>> demo1.name

'admin'

>>> 

class

user_info

(object):

...

def__init__

(self, name, pwd):

... self.__name = name

... self.__pwd = pwd

...

...

@property

...

defname

(self):

# 特性, 唯讀

...

return self.__name

...

@name.setter

...

defname

(self, val):

# 特性, 只寫

... self.__name = val

...

>>> u = user_info('root', 'admin')

>>> print(u.name)

root

>>> u.name = 'aaa'

# u.name實際是呼叫的上面的name方法,這種方式使得python**變得優雅

>>> print(u.name)

aaa>>>

實現 _ iter _ 和 _ next _ 函式

in的時候會呼叫 iter

for的時候會不斷呼叫 next

>>> 

import random

>>>

class

user_info

(object):

...

def__init__

(self, name, pwd):

... self.__name = name

... self.__pwd = pwd

... self.__loop = 10

...

...

def__iter__

(self):

... print('__iter__')

... self.__loop = 10

# 規定迭代次數

...

return self

...

...

def__next__

(self):

... print('__next__')

... ret = random.randint(1, 1000)

... self.__loop -= 1

...

ifnot self.__loop:

...

raise stopiteration() # 停止迭代

...

return ret

...

>>> u = user_info('root', 'admin')

>>>

for i in u: # for會接收到stopiteration的異常從而退出迴圈

... print(i)

...

__iter__

__next__

542__next__

498__next__

483__next__

353__next__

156__next__

893__next__

624__next__

682__next__

541__next__

>>>

類的高階特性

被定義為final的物件引用只能指向唯一乙個物件,不可以將它指向其他物件,但是乙個物件的值本身是可以改變的,為了使乙個常量做到真正不可更改,可以將常量宣告為 static final 全域性常量 乙個被定義為private的方法隱式的被指定為 final 型別final類不允許被繼承,不允許被改動 ...

Python高階特性

l csx sarah tracy bob jack print l 0 l 1 l 2 輸出 csx sarah tracy l 0 3 輸出 csx sarah tracy 如果第乙個索引是0,還可以省略 print l 2 輸出 bob jack print l 2 1 輸出 bob d fo...

Python高階特性

python 高階特性 from collections import iterable from collections import iterator 切片 list切片 l list range 100 print l 0 3 取前3個元素 print l 3 取前3個元素 print l 4...