In-depth analysis of Python Functions

Source: Internet
Author: User
Tags python list

Python is a clear language. Another meaning is that its authors intentionally design highly restrictive syntaxes to make bad programming habits, for Python functions, see some of its new and unique functions in the following article.

For more details about the Python function, please refer to the o'reilly book written by Mark Lutz in your local bookstore, or read the document with PythonGuido Rossum, the actual author of Python. The first interesting thing is that this application processes command lines. List sys. argv is a command line. To use it, you need to understand the Python list syntax. The syntax used in this example is very basic, but it is enough for you to understand this program.

Next, to facilitate debugging, define a MsgBox function. Note that the syntax used for function declaration and class method declaration is exactly the same. The only difference is that the class method requires the parameter self, which contains the reference of the object being called. Of course, you do not have to call it "self ". But if you do not, you will be confused .)

What is really interesting is the more complicated _ init _ method. Here, we did not build a simple menu with no control as in the previous example, but instead built a menu bar and attached the menu event to the callback routine, and build the window partition bar, tree control, and edit control.

You can read this to see how the whole job works. If you have not done GUI programming before, you will find it very difficult .) Visual Basic can do all of this in a behind-the-scenes way, but if you only open a table file in a text editor.

You can still see it. If you have used C/C ++ for GUI work, you will be familiar with it. Once a window is constructed, let's look at the actual code of the program. First, there are two methods for loading and saving project files after method _ init.

Then we can see how Python uses open to process file I/O. Note: In fact ,? How can I raise and restore my mind? -- As it occurs, for Python, the file handle is only a memory-managed object, and the object is referenced by the counter.

When the reference becomes invalid, the Python function will clear it and the file will be closed automatically. Files cannot be automatically closed if they cannot be fully trusted ). For example, you will open the written file again and read it. In this case, you must close it explicitly. This is a problem that is not discussed by C people ).

Another interesting feature of loading/saving functions is that they encounter the exception of IOError caused by bad file calls ). I asked the code to describe myself, but that's how you do it, kids. The remaining problems of the new code are the Event Handlers used to handle the problems encountered during the execution of the program. I once again let the program understand a large part. Note.

Common user interactions are handled using the wxMessageDialog, wxFileDialog, and wxTextEntryDialog dialogs. These calls are a little different from the conventional Windows programming usage. Here I will only give you some clues.

First, the dialog box is an object created by a reasonable call. It is displayed with ShowModal () and destroyed after use. Second, the button clicked by the user is returned from ShowModal () as the return value, you can use the attach method of the dialog box to obtain other values. For example, during file dialog box interaction, the path selected by the user is obtained as dlg. GetPath. This is quite different from how Windows APIs work. That's exactly what you know.

The document state tells me that wxWindows API documentation is ...... Under development. The biggest weakness of this platform is the lack of good documentation, which I am actively changing. The situation will be improved in the second half of the year. At the same time, Python itself has some good books, and the online documentation of Python functions is not bad. This document is good for wxWindows and C ++.

Sometimes, in the Python function, how to make these documents more reasonable is quite mysterious. There are some specific Python annotations, but in many cases. You will find that you must read the Demo code along with wxPython, or you must ask your questions to the experts in the email list. Fortunately, these experts are "traceable.

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.