其他内建功能示例

January 30, 2026

其他内建功能示例

本文档介绍 printin/not in、相等比较、is/is not、切片、max/min、乘法、异或、evalctypes 及调试、异常等用法。示例来源:doc/pikapython.com/examples/builtins/

模块简介

涵盖常用内建函数与运算符。内嵌示例:

print('hello', 42)
assert 1 in [1, 2, 3]
assert max(1, 2, 3) == 3

示例代码

打印输出(print.py)

print('test')
print('my name is', 'old wang', 'my age is', 43)
print('format: %s,%04d,%.2f' % ('test', 123, 15.5))

说明:多参数打印与 % 格式化。

in 操作符(contains.py)

# test for __contains__

assert 't' in 'test'
assert 'q' not in 'test'
assert 'te' in 'test'
assert 'tq' not in 'test'

assert b't' in b'test'
assert b'q' not in b'test'
assert b'te' in b'test'
assert b'tq' not in b'test'

assert bytearray(b't') in bytearray(b'test')
assert bytearray(b'q') not in bytearray(b'test')
assert bytearray(b'te') in bytearray(b'test')
assert bytearray(b'tq') not in bytearray(b'test')

assert 1 in range(10)
assert 10 not in range(10)

assert (1,) in [(1,), (2,)]
assert (1,) not in [(2,), (3,)]

assert 'a' in {'a': 1, 'b': 2}
assert 'c' not in {'a': 1, 'b': 2}

print('PASS')

说明:in/not in 用于字符串、字节、range、元组列表、字典键等。

相等性比较(eq.py)

assert [1, 2, 3] == [1, 2, 3]
assert [1, 2, 3] != [1, 2, 4]

assert (1, 2, 3) == (1, 2, 3)
assert (1, 2, 3) != (1, 2, 4)

assert (1, 2, 3) != [1, 2, 3]
assert [1, 2, 3] != (1, 2, 3)
assert [1, 2, 3] != 1

assert 1 != (1, 2, 3)

assert ('a', 'b', 'c') == ('a', 'b', 'c')
assert ('a', 'b', 'c') != ('a', 'b', 'd')

dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'a': 1, 'b': 2, 'c': 3}
dict3 = {'a': 1, 'b': 2, 'c': 4}

assert dict1 == dict2
assert dict1 != dict3

print("PASS")

说明:列表、元组、字典的 ==/!= 按值比较;不同类型(如 list 与 tuple)不相等。

is 和 is not(is_not.py)

assert (1 is not 1) == False
assert (1 is not 2) == True
assert (1 is not None) == True
assert (None is not None) == False
assert (None is not 1) == True
assert (1 is not 1.0) == True
assert (1.0 is not 1) == True
assert (1.0 is not 1.0) == False
print("PASS")

说明:is/is not 比较对象身份,与 == 不同;对 None 和数值类型的使用需注意运行环境差异。

切片与索引(slice.py)

my_string = "Hello, World!"
out_of_range = False

# 测试索引超出字符串长度的情况
try:
    my_string[len(my_string)] == ""
except:
    print("1.IndexError: 索引超出字符串长度")
    out_of_range = True

assert out_of_range

# 测试切片超出字符串长度的情况
b = my_string[0:100]
print(b)

# 测试使用负数索引访问字符串的情况
assert my_string[-1] == "!"
assert my_string[-5:-1] == "orld"
print("PASS")

说明:越界索引触发异常;切片可超出长度(截断);负索引表示从末尾计数。

max/min(max_min.py)

# 测试 max() 函数
assert max(1, 2, 3) == 3, "max() 函数错误: max(1, 2, 3) 应返回 3"
assert max(-1, -2, -3) == -1, "max() 函数错误: max(-1, -2, -3) 应返回 -1"
assert max(5, 5) == 5, "max() 函数错误: max(5, 5) 应返回 5"
assert max([1, 2, 3, 4]) == 4, "max() 函数错误: max([1, 2, 3, 4]) 应返回 4"

# 测试 min() 函数
assert min(1, 2, 3) == 1, "min() 函数错误: min(1, 2, 3) 应返回 1"
assert min(-1, -2, -3) == -3, "min() 函数错误: min(-1, -2, -3) 应返回 -3"
assert min(5, 5) == 5, "min() 函数错误: min(5, 5) 应返回 5"
assert min([1, 2, 3, 4]) == 1, "min() 函数错误: min([1, 2, 3, 4]) 应返回 1"

print('PASS')

说明:max/min 支持多参数或单参数可迭代对象(如列表)。

乘法操作(mult.py)

# mult.py
assert 1 * 2 == 2
assert 1 * 2 * 3 == 6
assert 1 * 2 * 3 * 4 == 24
assert 'a' * 3 == 'aaa'
assert 'a' * 3 * 2 == 'aaaaaa'
assert 3 * 'bb' == 'bbbbbb'
assert 3 * 'bb' * 2 == 'bbbbbbbbbbbb'
assert 3 * 'bb' * 2 * 3 == 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
assert b'x' * 3 == b'xxx'
assert b'x' * 3 * 2 == b'xxxxxx'
assert 3 * b'yy' == b'yyyyyy'
assert 3 * b'yy' * 2 == b'yyyyyyyyyyyy'

assert [1,2,3] == [1,2,3]
assert [1,2,3] != [1,2,4]
assert 2 * [1, 2, 3] == [1, 2, 3, 1, 2, 3]
assert [1, 2, 3] * 2 == [1, 2, 3, 1, 2, 3]
assert 3 * [1, 2, 3] == [1, 2, 3, 1, 2, 3, 1, 2, 3]
assert [1, 2, 3] * 3 == [1, 2, 3, 1, 2, 3, 1, 2, 3]

print('PASS')

说明:整数乘法;字符串/字节与整数相乘为重复;列表与整数相乘为重复拼接。

异或(XOR.py)

# '6' in binary is '110', '3' in binary is '011'.
# Bitwise XOR operation: '110' ^ '011' = '101' = '5' in decimal
assert 6 ^ 3 == 5

# '10' in binary is '1010', '6' in binary is '0110'.
# Bitwise XOR operation: '1010' ^ '0110' = '1100' = '12' in decimal
assert 10 ^ 6 == 12

# Start with '6' ('110' in binary)
value = 6

# Bitwise XOR and assign with '3' ('011' in binary), '110' ^ '011' = '101'
value ^= 3
assert value == 5

# Bitwise XOR and assign with '10' ('1010' in binary), '101' ^ '1010' = '1111'
value ^= 10
assert value == 15


print('PASS')

说明:^ 为按位异或,^= 为异或并赋值。

eval(eval.py)

def bar():
    # local scope
    local = 5
    assert eval('local') == 5


bar()

assert eval('1+1') == 2


def foo():
    return 3


# global scope
assert eval('foo()') == 3

g_val = 4
assert eval('g_val') == 4


print('PASS')

说明:eval(字符串) 在当前作用域执行表达式,可访问局部与全局变量;设备上是否支持以运行环境为准。

ctypes 缓冲区(ctypes.py)

import ctypes
read_data = ctypes.c_buffer(b'', 16)
print("----read size----")
datalen = len(read_data.raw)
print(datalen)
print(read_data.raw)
print("----read data----")
for i in range(0,datalen):
    print(read_data.raw[i])

说明:c_buffer(初值, 长度) 创建固定长度缓冲区,.raw 访问底层字节;依赖运行环境是否提供 ctypes 模块。

调试断点(pdb_set_break.py)

import PikaDebug as pdb
pdb.set_trace()
pdb.set_break('pdb_set_break', 48)
print('line 1')
print('line 2')
print('line 3')
print('line 4')
print('line 5')

说明:使用 PikaDebug 设置断点与跟踪,具体行号和模块名需与运行环境匹配;OP-BTS 上可能需相应调试支持。

初始化中抛出异常(init_raise.py)

class Test:
    def __init__(self):
        raise


t = Test()

说明:在 __init__ 中执行 raise 会使构造失败,后续 t = Test() 会抛出异常;可用于校验构造参数或资源是否可用。

注意事项

  • evalctypesPikaDebug 等是否可用取决于固件与模块配置。
  • 在嵌入式环境中慎用 eval,避免执行不可信字符串。

相关链接