python类的实例方法必须创建对象前还是后可以调用_classmethod可以来调用类的属性,类的方法,实例化对象...
classmethod可以來調用類的屬性,類的方法,實例化對象,今天番茄加速就來講一下。
classmethod()
classmethod 修飾符對應的函數不需要實例化,不需要 self 參數,但第一個參數需要是表示自身類的 cls 參數,可以來調用類的屬性,類的方法,實例化對象等。
In [66]: class Student():
...: def __init__(self,id,name):
...: self.id = id
...: self.name = name
...: def __repr__(self):
...: return 'id = '+self.id +', name = '+self.name
...: @classmethod
...: def f(cls):
...: print(cls)
complie()
將字符串編譯成python 能識別或可以執行的代碼,也可以將文字讀成字符串再編譯。
In [74]: s = "print('helloworld')"
In [75]: r = compile(s,"", "exec")
In [76]: r
Out[76]: at 0x0000000005DE75D0, file "", line 1>
In [77]: exec(r)
helloworld
complex()
創建一個復數
In [81]: complex(1,2)
Out[81]: (1+2j)
delattr()
刪除對象的屬性
In [87]: delattr(xiaoming,'id')
In [88]: hasattr(xiaoming,'id')
Out[88]: False
dict()
創建數據字典
In [92]: dict()
Out[92]: {}
In [93]: dict(a='a',b='b')
Out[93]: {'a': 'a', 'b': 'b'}
In [94]: dict(zip(['a','b'],[1,2]))
Out[94]: {'a': 1, 'b': 2}
In [95]: dict([('a',1),('b',2)])
Out[95]: {'a': 1, 'b': 2}
dir()
不帶參數時返回當前范圍內的變量,方法和定義的類型列表;帶參數時返回參數的屬性,方法列表。
In [96]: dir(xiaoming)
Out[96]:
['__class__',
'__delattr__',
'__dict__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__le__',
'__lt__',
'__module__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'name']
divmod()
分別取商和余數
In [97]: divmod(10,3)
Out[97]: (3, 1)
總結
以上是生活随笔為你收集整理的python类的实例方法必须创建对象前还是后可以调用_classmethod可以来调用类的属性,类的方法,实例化对象...的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: jmeter 线程组与参数_jmeter
- 下一篇: apache iotdb_Apache-
