Python basics tutorial notes-Project 1-instant markup-Day3, python-day3

Source: Internet
Author: User

Python basics tutorial notes-Project 1-instant markup-Day3, python-day3

Yesterday we implemented a simple conversion of txt to html, and today we are going further.

The following describes parameters with asterisks, getattr functions, and callable functions.

First look at the Handler class:

class Handler:    def callback(self, prefix, name, *args):        method = getattr(self, prefix+name, None)        if callable(method): return method(*args)    def start(self, name):        self.callback('start_', name)    def end(self, name):        self.callback('end_', name)    def sub(self, name):        def substitution(match):            result = self.callback('sub_', name, match)            if result is None: match.group(0)            return result        return substitutiondef sub_test(self, name):        print name

Learn about the callback function today

First look at the callback Parameters

What does a parameter with * mean?

Here is a piece of code:

def calc(*numbers):sum = 0for i in numbers:sum = sum + ireturn sumresult = calc(1,2,3)temp = calc()print resultprinta temp

The execution result is:

That is to say, the parameter with * represents a variable number of parameters (the length can be 0 ). For details, see the link:

Http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001374738449338c8a122a7f2e047899fc162f4a7205ea3000

Then getattr

The official document explains getattr () as follows:
Getattr (object, name [, default])

Return the value of the namedattriof of object. name must be a string. if the string is the name of one ofthe object's attributes, the result is the value of that attribute. forexample, getattr (x, 'foobar') is equivalent to x. foobar. if the named attributedoes not exist, default is returned if provided, otherwise AttributeError israised.

That is, if the object contains an attribute named name (name must be a string), this attribute is returned. If this attribute is not found in the object and the default parameter is provided in getattr, the default parameter is returned. Otherwise, an AttributeError error is thrown.

Let's look at the example below:

lass Test:def print_hello(self):print 'func print_hello work'a = Test()b = getattr(a,'print_hello',None)print bb()b = getattr(a,'NoFunc',None)print b

The execution result is:



When using the getattr function for the first time, B tries to obtain the print_hello function in a. From the printed result, we can see that B is called after the acquisition is successful.

When using the getattr function for the second time, B tries to obtain the NoFunc function in a. The printed result shows that a does not have a function named NoneFunc, so the value of B is None.

Next, let's look at the callable function.

View chart


Obviously, if the callable parameter is the function name, True is returned; otherwise, False is returned. In addition, you can see that func () is executed once in the command line. Function Execution occurs in the process of a = func ().

In summary, the callback (self, prefix, name, * args) function is:

Query whether the callback function has a function named 'prefix + name' in the callback function object. If this function is available, the function is called and the execution result of the function is returned. (The function name is prefix + name, and the parameter is * args ). Run the following code:

class CalTest:def callback(self,prefix,name,*args):method = getattr(self,prefix+name,None)if callable(method):return method(*args)def printSum(self,*arg):sum = 0for i in arg:sum = sum + ireturn suma = CalTest();b = a.callback('print','Sum',1,2,3)print bb = a.callback('print','Sum')print b
The execution result is:



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.