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
Sey0
V2EX  ›  Python

Python 传入参数为字符串,赋值以后自动转为 tuple?有大哥遇到过这奇怪的问题吗

  •  
  •   Sey0 · 2019-03-24 23:28:38 +08:00 · 2568 次点击
    这是一个创建于 1831 天前的主题,其中的信息可能已经有所发展或是发生改变。

    上传不了图片,用文字来说明吧

    self.website = website

    我赋值 website 为'360',self.website 会变成('360',)

    找了很久的原因没有找到,有大哥遇到过吗

    第 1 条附言  ·  2019-03-25 00:25:40 +08:00

    v2没找到上传图的地方 代码:print("self.website:", self.website, 'website:',website);exit(); 直接结果: self.website: ('360',) website: 360

    第 2 条附言  ·  2019-03-25 00:31:13 +08:00
    第 3 条附言  ·  2019-03-25 00:32:55 +08:00
    12 条回复    2019-03-25 10:36:40 +08:00
    xiexingjia
        1
    xiexingjia  
       2019-03-24 23:36:36 +08:00
    贴一下代码呀
    shintendo
        2
    shintendo  
       2019-03-24 23:38:28 +08:00
    请给一段最短的可复现程序
    ipwx
        3
    ipwx  
       2019-03-24 23:38:57 +08:00
    你不会写了 self.website, 吧。。。

    后面加个逗号就变成 tuple 了。
    ipwx
        4
    ipwx  
       2019-03-24 23:39:08 +08:00
    >>> a = 'a',
    >>> a
    ('a',)
    Fulcrum
        5
    Fulcrum  
       2019-03-24 23:41:17 +08:00 via Android
    不要逗号,就算廖雪峰入门的教程都说了
    shintendo
        6
    shintendo  
       2019-03-24 23:42:06 +08:00
    这种问题多半是在你认为绝对没问题的地方出了问题,所以代码贴完整很重要
    ipwx
        7
    ipwx  
       2019-03-25 00:30:00 +08:00
    。。大哥你能不能把完整的代码贴上来?

    贴代码用 gist
    junjieyuanxiling
        8
    junjieyuanxiling  
       2019-03-25 00:34:46 +08:00 via Android   ❤️ 1
    倒数第二行末尾逗号删了。
    Sey0
        9
    Sey0  
    OP
       2019-03-25 00:36:47 +08:00
    @junjieyuanxiling - -瞎了眼硬是没看到 一直以为是赋值的问题,感谢大哥
    Trim21
        10
    Trim21  
       2019-03-25 00:40:08 +08:00 via Android
    感觉需要有一个在括号外 no trailng comma 的规则…不过之前看了看 pylint 和 pycodestyle 都没有类似的规则…
    freakxx
        11
    freakxx  
       2019-03-25 01:31:13 +08:00
    self.website =
    这行结尾多了一个逗号
    yushenglin
        12
    yushenglin  
       2019-03-25 10:36:40 +08:00
    大哥,python 严格严格遵循 PE8 规则,只靠缩进控制,你在 website 后面加了个, ,肯定变元组啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2905 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 13:57 · PVG 21:57 · LAX 06:57 · JFK 09:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.