V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
eloah
V2EX  ›  Python

奇怪的 Python 特性

  •  1
     
  •   eloah · 2017-09-01 14:50:58 +08:00 · 2817 次点击
    这是一个创建于 2400 天前的主题,其中的信息可能已经有所发展或是发生改变。

    好吧,是一个 feature 还是 bug 已经搞不清了

    Ubuntu 16.04.03 Python3.5.2

    >>> a = ''
    >>> a[1:-1]
    ''
    >>> a[2:-1]
    ''
    >>> a[3:-1]
    ''
    
    
    13 条回复    2017-09-02 10:16:00 +08:00
    clino
        1
    clino  
       2017-09-01 14:52:03 +08:00
    你觉得应该返回什么?
    eloah
        2
    eloah  
    OP
       2017-09-01 14:54:57 +08:00
    @clino
    raise IndexError
    013231
        3
    013231  
       2017-09-01 15:12:06 +08:00
    是 feature。
    https://docs.python.org/3.6/library/stdtypes.html#common-sequence-operations

    s[i:j:k]
    “ The slice of s from i to j is defined as the sequence of items with index k such that i <= k < j. If i or j is greater than len(s), use len(s). If i is omitted or None, use 0. If j is omitted or None, use len(s). If i is greater than or equal to j, the slice is empty.”
    est
        4
    est  
       2017-09-01 15:17:23 +08:00
    In [457]: a='a'

    In [458]: a[0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0]
    Out[458]: 'a'
    mark06
        5
    mark06  
       2017-09-01 15:18:25 +08:00
    > However, out of range slice indexes are handled gracefully when used for slicing:

    ```
    >>> word[4:42]
    'on'
    >>> word[42:]
    ''
    ```
    [document]( https://docs.python.org/3.5/tutorial/introduction.html#strings)
    SuperMild
        6
    SuperMild  
       2017-09-01 15:36:11 +08:00
    动态语言一般倾向于默认一种行为以方便使用,而不是抛出异常,抛出异常还要处理异常多麻烦啊。动不动就抛出异常那是静态语言的习惯。
    LeeSeoung
        7
    LeeSeoung  
       2017-09-01 15:42:44 +08:00
    说实话,要是避免抛异常你是不是还要多写几个判断边界的语句,但是写完判断边界后你觉得是这种特性好用,还是你自己再写一堆代码好用。。
    eloah
        8
    eloah  
    OP
       2017-09-01 15:50:45 +08:00
    @est
    这个死循环反而很好理解啊
    eloah
        9
    eloah  
    OP
       2017-09-01 15:54:30 +08:00
    @SuperMild
    @LeeSeoung
    可以理解的,写代码起来方便
    但是不抛异常可能会导致错误被隐藏起来啊
    clino
        10
    clino  
       2017-09-01 16:00:50 +08:00
    @eloah 判断返回值是否空不就行了?
    winglight2016
        11
    winglight2016  
       2017-09-01 20:47:44 +08:00
    @eloah 这大概是语言背后的哲学不同,我习惯 java 就很难理解这种 feature
    jedihy
        12
    jedihy  
       2017-09-02 07:36:11 +08:00
    一直都是这样啊
    popbones
        13
    popbones  
       2017-09-02 10:16:00 +08:00
    异常不一定带表错误,是否检查以及如何处理异常是程序逻辑,程序员需要根据需要使用相应的语法功能。

    这就好比什么时候用 if a 什么时候用 if a is not None 是程序员的责任而不是语言的责任一样。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1083 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 22:47 · PVG 06:47 · LAX 15:47 · JFK 18:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.