Python2 lambda internal value assignment printing method

Source: Internet
Author: User

Lambda expressions of python2 are very useful, but they are magical in the form of python2. They cannot print, raise, or assign values ). The following improvements are made to the lambda expression peripheral products based on python2.73 @ ubuntu default, selling a good price :).

In python2, exec can be used, but it is ugly, so it defines several functions, use it.


[Python] #! ...
Def raise _ (ex ):
Raise ex
Def print _ (* l ):
For I in l:
Print I,
Def defval (var, val ):
Globals () [var] = val
Return val
 
(Lambda: defval ('A', 3) and a = 3 and type (a) or 2 )()
(Lambda: defval ('A', 2) and a = 3 and type (a) or 2 )()
(Lambda x: print _ (x) ('World ')
(Lambda: raise _ (Exception ('Hi ')))()

#! ...
Def raise _ (ex ):
Raise ex
Def print _ (* l ):
For I in l:
Print I,
Def defval (var, val ):
Globals () [var] = val
Return val

(Lambda: defval ('A', 3) and a = 3 and type (a) or 2 )()
(Lambda: defval ('A', 2) and a = 3 and type (a) or 2 )()
(Lambda x: print _ (x) ('World ')
(Lambda: raise _ (Exception ('Hi ')))()
In general, I do not advocate "awesome"... but sometimes it is really much more convenient, and lambda is a super-God in an instant. Gao fushuai =. =.

 

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.