天天看點

python内置函數簡單示例(二)

文章目錄

        • chr(i)
        • @classmethod 、 @staticmethod
        • compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)
        • class complex([real[, imag]])
        • delattr(object, name)、getattr(object, name[, default]) 、 setattr(object, name, value)
        • 參考文檔

    關于内置函數的解釋說明請查閱官網,本文主要提供簡單示例。适用于python3.8。

chr(i)

>>> chr(65)     
'A'
>>> chr(0x0041)
'A'
           

可參考線上Unicode編碼表

@classmethod 、 @staticmethod

    classmethod() ,用于建立備用類構造函數的變體。Python中的staticmethod與Java或C ++中的靜态方法類似。

    編寫簡單示例子產品method.py:

class test_method:
    name="function_out"
    addr="test_method"
    def __init__(self,name,addr):
        self.name=name
        self.addr=addr
        print(self.name,self.addr)
        name="function_inside"
        addr="__init__"

    @classmethod
    def info_cls(cls,name,addr):
        print("The classmethod test: ",cls.name,cls.addr)

    @staticmethod
    def info_stc(name,addr):
        print("The staticmethod test: ",name,addr)
           

    導入子產品,進行簡單測試

>>> from method import *
>>> test_method.info_cls("Tom","西二旗")
The classmethod test:  function_out test_method
>>> test_method.info_stc("Tom","西二旗")
The staticmethod test:  Tom 西二旗
           

    建立對象,進行測試

>>> test1=test_method("Rose","上地")
Rose 上地
>>> test1.info_cls("Jack","五道口") 
The classmethod test:  function_out test_method
>>> test1.info_stc("Jack","五道口") 
The staticmethod test:  Jack 五道口
           

    可以看到類方法使用的cls.name為function_out,而不是function_inside。如果info_cls修改為:

def info_cls(cls,name,addr):
    cls.name=name
    cls.addr=addr
    print("The classmethod test: ",cls.name,cls.addr)
           

    則

>>> test_method.info_cls("Tom","西二旗")
The classmethod test:  Tom 西二旗
           

compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)

    示例來自網絡

示例

>>> import ast
>>> ast_object=ast.parse("print('Hello world!')")
>>> print(type(ast_object))
<class '_ast.Module'>
>>> code=compile(ast_object,filename="",mode="exec")
>>> print(type(code))
<class 'code'>
>>> exec(code)
Hello world!
           

示例

>>> x=5
>>> code=compile('x==5','','eval')
>>> result=eval(code)
>>> print(result)
True
>>> 
>>> code=compile('x+5','','eval')
>>> result=eval(code)
>>> print(result)
10
           

class complex([real[, imag]])

    傳回值為 real + imag*1j 的複數,或将字元串或數字轉換為複數。

>>> complex(2,3)
(2+3j)
           

    我們可以重新編寫子產品,當輸入complex(2,3)時,輸出(3+2j)

編寫子產品complex.py

class test(object):
    def __init__(self,a,b):
        self.a=a
        self.b=b

    def __complex__(self):
        return(complex(self.b,self.a))
           

    導入子產品并驗證一下

>>> from complex import *
>>> test1=test(2,3)      
>>> complex(test1)       
(3+2j)
           

    這樣做有個缺點,因為complex()第二個形參不能是字元串,是以沒辦法對輸入的字元串進行處理,可以對子產品進行如下簡單改進

class test(object):
    def __init__(self,a,b):
        self.a=a
        self.b=b

    def __complex__(self):
        if type(self.a) == str :
            return(complex(self.a))
        else:
            return(complex(self.b,self.a))
           

    如果輸入的第一個參數為字元串,則變成複數

>>> from complex import *
>>> test2=test("1+2j","")
>>> complex(test2)       
(1+2j)
           

    這樣做的缺點就是建立對象的時候必須要輸入兩個參數,還有很大的改進空間。

delattr(object, name)、getattr(object, name[, default]) 、 setattr(object, name, value)

    編寫一個測試子產品attr.py

class dynasty(object):
    def __init__(self,dyname):
        self.dyname=dyname
        print("The dynasty's name is ",self.dyname)

    def person(self,name):
        self.name=name

    def weapon(self,weapon):
        self.weapon=weapon
           

    導入子產品并指派

>>> from attr import *
>>> dy=dynasty("漢")  
The dynasty's name is  漢
>>> dy.person("劉備")
>>> dy.weapon("雙劍")
           

    可以檢視指派

>>> getattr(dy,"dyname")
'漢'
>>> getattr(dy,"name")
'劉備'
>>> getattr(dy,"weapon")
'雙劍'
           

    删除某值

>>> delattr(dy,"weapon")
>>> getattr(dy,"weapon")
<bound method dynasty.weapon of <attr.dynasty object at 0x7f0ce5a51f70>>
           

    重新指派

>>> setattr(dy,"weapon","雙截棍")
>>> getattr(dy,"weapon")         
'雙截棍'
           

參考文檔

https://docs.python.org/zh-cn/3/library/functions.html

https://www.journaldev.com/22772/python-compile-function