python内置函数简单示例(二)

    关于内置函数的解释说明请查阅官网,本文主要提供简单示例。适用于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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值