dongshao2967 2014-02-20 15:10
浏览 39

从php到python - 没有大括号的交易

i am a PHP developer by... well that's just what i picked up from a need to develop web services. since than i've found a need to start using python for various projects. one of the first things that has pissed me off is the obsession "properly" indenting code. now i understand its purpose. however, what i have found is that not using { } in my code blocks tends to cause me some periods of wtf.

because of this, i find myself doing stuff like

if a is not None:
  for x in a:
      print x
  # endfor
# endif

this seems to help me keep my code in order and visually i can see whats in scope to the loop and control statement.

so what are some other people doing out there that come from the land of { and ; ? or am i the only maniac with this issue?

  • 写回答

3条回答 默认 最新

  • dsdtszi0520538 2014-02-20 16:29
    关注

    1 )

    do not do

    from __future__ import braces

    python will troll you with the following if you do

    >>> from __future__ import braces
    SyntaxError: not a chance
    

    basically python is telling you no because python is not coded for php and javascript like syntax

    its coded to tell you no

    to python coders/devs its considered an inside joke but its cruel to tell someone to do that when they are asking for help

    2) (if your using python 2.7.5 or older than your fine)

    for python 3.x your code is broken as it will return a syntax error

    >>> a = "hsfjsdhfjdhfdhfdhf"
    >>> if a is not None:
            for x in a:
                    print x
    
    SyntaxError: invalid syntax
    

    you forgot ()

    it should look like this

    >>> a = "hsfjsdhfjdhfdhfdhf"
    >>> if a is not None:
            for x in a:
                    print(x)
    
    h
    s
    f
    j
    s
    d
    h
    f
    j
    d
    h
    f
    d
    h
    f
    d
    h
    f
    

    also {} is a dict in python and since python has strict syntax rules you wont be able to wrap code in it only strings in the following formats

    {"lol":"omg"}
    {"lol":"omg","wow":"omg"}
    {"lol":{"wow":"omg","pfft":"lolwut"}}
    {"lol":["lol","omg","pfft","lolwut"]}
    {"lol":[["lol","omg"],["pfft","lolwut"]]}
    

    and numerous other ways

    also you can infinetly place a dict inside a dict as long as a key exists the value can be a dict, list, tuple, or string

    example:

    {"lol":{"wow":{"omg":{"pfft":"lolwut"}}}}
    

    only thing is

    it gets harder to split that if you dont know what to do

    3)

    the one thing i recommend you do is get a nice big book on python and start reading

    that's the best way to learn python

    especially if you want your projects to be good

    or if you want to learn online

    start with this

    http://learnpythonthehardway.org/

    and then move onto this

    Datastructures and Algorithms (below is the link)

    http://interactivepython.org/courselib/static/pythonds/index.html

    or you can do both it doesnt really matter which order you do it in so long as you learn something

    评论

报告相同问题?

悬赏问题

  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败