The Seven Year Itch.

MENU

Python partition使用技巧

November 22, 2018 • Read: 153 • Python

partition() 方法用来根据指定的分隔符将字符串进行分割。
如果字符串包含指定的分隔符,则返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。

  • flask源代码的run模块中,出现的代码当做示例
def run():
    ......
    _host = '127.0.0.1'
    _port = 5000
    server_name = self.config.get('SERVER_NAME')
    sn_host, sn_port = None, None
    
    if server_name:
        sn_host, _, sn_port = server_name.partition(':')

SERVER_NAME 是在setting文件设置的

SERVER_NAME = '127.0.0.1:6666'

SERVER_NAME拆分开,我们简化Flask的源代码

server_name = "127.0.0.1:6666"

s_host, _, s_port = server_name.partition(":")

print(s_host, s_port)

print(s_host, _, s_port)

结果 >>>>

127.0.0.1 6666

127.0.0.1 : 6666
Tags: None
Archives QR Code
QR Code for this page
Tipping QR Code