[Python 学习]2.5版yield之学习心得 - limodou的学习记录 - limodou是一个程序员,他关心的焦点是Python, DocBook, Open Source … 在 shhgs 发布了关于《 Py 2.5 what’s new 之 yield 》之后,原来我不是特别关注 yield 的用法,因为对于2.3中加入的yield相对来说功能简单,它是作为一个 generator 不可缺少的一条语句,只要包含它的函数即是一个 generator 。但在2.3中,generator 不能重入,不能在运行过程中修改,不能引发异常,你要么是顺序调用,要么就创建一个新的 generator。而且 generator 中的 yield 只是一个语句。但到了 2.5 版之后,情况发生了很在的变化。 在 shhgs 的文章中对于 yield 并没有做太多的描述,也因此让我在理解上产生了许多问题,于是我仔细地研究了 What’s new 和 PEP 342 文档,有了一些体会,描述在下面。 这里不说为什么要对 yield 进行修改,只说功能。 1. yield 成为了表达式,它不再是语句,但可以放在单独的行上。 Redefine "yield" to be an expression, rather than a statement. 可以看到,如果你还是写成语句形式的话,其实还是一个表达式,只是它的值被扔掉了。 那么一个 yield 表达式可以这样写: x = yield i y = x + (yield x) 那么这种机制到底是如何工作的呢? 2. Add a new send() method for generator-iterators, which resumes the generator and "sends" a value that becomes the result of the current yield-expression. 执行一个 send(msg) 会恢复 generator 的运行,然后发送的值将成为 当前 yield 表达式的 返回值 。 那么可以看过这其实包含了一次运行,从将msg赋给当前被停住的 yield 表达式开始,到下一个 yield 语句结束,然后返回下一个yield语句的参数,然后再挂起,等待下一次的调用。 那么让我们开始想象一下,把 yield 转变为易于理解的东西吧。 我们可以把 yield 想象成下面的伪代码: x = yield i ==> put(i); x = wait_and_get() 看见什么了。 3.
Python Software Foundation News labs :: Python beginner's mistakes Every Python programmer had to learn the language at one time, and started out as a beginner. Beginners make mistakes. This article highlights a few common mistakes, including some I made myself. Beginner's mistakes are not Python's fault, nor the beginner's. They're merely a result of misunderstanding the language. To put it another way, the mistakes in this article are often cases of "the wrong tool for the job", rather than coding errors or sneaky language traps. Mistake 1: trying to do low-level operations Python is sometimes described as a VHLL, a Very High-Level Language. This doesn't mean that it isn't possible to do these things with Python; but it's probably just not the right language for these jobs. Mistake 2: writing "language X" code in Python This is a mistake that is almost unavoidable. Some notorious symptoms of "language X" code, and the languages that may cause them: The point here is not to slam the language that you're used to (although that is always fun ;-).
Hitchhiker's Guide to Python « late.am I first heard about The Hitchhiker’s Guide to Python at PyCodeConf a few months ago. It’s a fantastic idea: open source, community-driven documentation on how to do Python right: everything from how to learn Python, to how to write idiomatic code, to how to distribute your projects, to surveys of best-of-breed open source projects and libraries you can build projects and applications on top of. Many many thanks to Kenneth Reitz for creating and maintaining the project, which is hosted at GitHub. At this time, the Hitchhiker’s guide is a little rough around the edges: many sections are only outlined, and need content written; other sections may not even exist yet. This sort of undertaking is effectively impossible for one person to maintain—one person can’t possibly know of every project, library, and idiom. Finally, since this is a community effort, I want to give a shout-out to all those who’ve contributed to the Guide so far:
The Hitchhiker’s Guide to Python! — pythonguide 0.0.1 documentation Greetings, Earthling! Welcome to The Hitchhiker’s Guide to Python. This is a living, breathing guide. If you’d like to contribute, fork us on GitHub! This handcrafted guide exists to provide both novice and expert Python developers a best practice handbook to the installation, configuration, and usage of Python on a daily basis. This guide is opinionated in a way that is almost, but not quite, entirely unlike Python’s official documentation. Note The use of Python 3 is highly preferred over Python 2. Let’s get started! Getting Started with Python New to Python? Properly Install Python on your system: Using Virtualenvs with Pipenv: Python Development Environments This part of the guide focus on the Python development environment, and the best-practice tools that are available for writing Python code. Writing Great Python Code This part of the guide focuses on the best-practices for writing Python code. Scenario Guide for Python Applications Shipping Great Python Code Additional Notes
BeginnersGuide New to programming? Python is free and easy to learn if you know where to start! This guide will help you to get started quickly. Chinese Translation/中文版入门 New to Python? Read BeginnersGuide/Overview for a short explanation of what Python is. Getting Python Next, install the Python 3 interpreter on your computer. There are also Python interpreter and IDE bundles available, such as Thonny. At some stage, you'll want to edit and save your program code. Learning Python Next, read a tutorial and try some simple experiments with your new Python interpreter. If you have never programmed before, see BeginnersGuide/NonProgrammers for a list of suitable tutorials. Most tutorials assume that you know how to run a program on your computer. Some sites offer in-browser coding for those who want to learn Python: Print a cheat sheet of the most important Python features and post it to your office wall until you know the basics well. Need Help? Need help with any of this? Complete list of Beginner's Guide pages
File Management in Python Introduction The game you played yesterday uses files to store game saves. The order you placed yesterday was saved in a file. File management is an important part of many applications written in nearly every language. Reading and Writing The most basic tasks involved in file manipulation are reading data from files and writing data to files. fileHandle = open ( 'test.txt', 'w' ) The "w" indicates that we will be writing to the file, and the rest is pretty simple to understand. fileHandle.write ( 'This is a test. This will write the string "This is a test." to the file's first line and "Really, it is." to the file's second line. fileHandle.close() As you can see, it's very easy, especially with Python's object orientation. fileHandle = open ( 'test.txt', 'a' )fileHandle.write ( '\n\n\nBottom line.' )fileHandle.close() Now let's read our file and display the contents: fileHandle = open ( 'test.txt' )print fileHandle.read()fileHandle.close() Only the second line is displayed.
web2py I believe that the ability to easily build high quality web applications is of critical importance for the growth of a free and open society. This prevents the biggest players from monopolizing the flow of information. Hence I started the web2py project in 2007, primarily as a teaching tool with the goal of making web development easier, faster, and more secure. Over time, it has managed to win the affection of thousands of knowledgeable users and hundreds of developers. Our collective effort has created one of the most full-featured Open Source Web Frameworks for enterprise web development. As a result, in 2011, web2py won the Bossie Award for best Open Source Development Software, and in 2012 it won the Technology of the Year award from InfoWorld. As you will learn in the following pages, web2py tries to lower the barrier of entry to web development by focusing on three main goals: Ease of use. Rapid development. Security.
keybr.com - Take typing speed test and practice typing online - StumbleUpon Lesson 10 - File I/O Introduction Last lesson we learnt how to load external code into our program. Without any introduction (like what I usually have), let's delve into file input and output with normal text files, and later the saving and restoring of instances of classes. (Wow, our lingo power has improved greatly!) Opening a file To open a text file you use, well, the open() function. Code Example 1 - Opening a file openfile = open('pathtofile', 'r') openfile.read() That was interesting. Seek and You Shall Find Did you try typing in print openfile.read()? whence is optional, and determines where to seek from. offset decribes how far from whence that the cursor moves. for example: openfile.seek(45,0) would move the cursor to 45 bytes/letters after the beginning of the file.openfile.seek(10,1) would move the cursor to 10 bytes/letters after the current cursor position.openfile.seek(-77,2) would move the cursor to 77 bytes/letters before the end of the file (notice the - before the 77) Try it out now. Nifty, eh?
Python Programming in your Browser: PythonAnywhere 10 Puzzle Websites to Sharpen Your Programming Skills Solving programming puzzles is a fun way to develop your logical and problem solving abilities. Also, when you’re familiarizing yourself with a new programming language, solving puzzles for that language can help speed up the learning process. Here are the top 10 popular programming puzzle sites that will help test your thinking and improve your programming, problem solving, and logical thinking skills. 1. Programming Praxis Programming Praxis is a blog that includes a range of interesting problems with solutions usually available in several different programming languages. 2. CodeKata is a blog of programming puzzles written by Dave Thomas, who’s most famous for the groundbreaking book, Pragmatic Programmer. 3. TopCoder is an active programming community of developers who love to solve puzzles. 4. 5. Facebook has a collection of very challenging programming puzzles that–should you manage to solve them–could result in you getting a job at Facebook! 6. 7. 8. 9. 10. 99 Prolog Problems