python __all__ yield 字符串一些操作

2020-09-18  本文已影响0人  明明就_c565

1,__all__是一个字符串list,用来定义模块中对于from XXX import *时要对外导出的符号,即要暴露的借口,但它只对import *起作用,对from XXX import XXX不起作用。

test.py

__all__=['x','y','test']

x=2

y=3

z=4

def test():

    print('test')

使用 for test import *  x y test直接可用 但是要想使用z 必须为from test import z

2,python字符串对齐

‘hello’.rjust(20,'*')

定义字符串长度为20,'hello'字符对齐到右边,前面填充‘*’

ljust()和rjust()类似,为对齐到左边

center()类似,对齐中间

3,python字符串去掉空格

lstrip() 去掉左边的空格或其他字符  lstrip(‘*’)

rstrip() 去掉右边的空格或其他字符  lstrip(‘#’)

4,python yield 使用 yield用于迭代器

带yield的函数是一个生成器

举例:

def test():

    for i in range(5):

        yield i*100 + i

for i in test():

    print(i)

输出 101 202 303 404

上一篇 下一篇

猜你喜欢

热点阅读