Yesterday implemented a simple TXT-to-HTML, a step further today.
Main understanding of the 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
Understand the next callback function today
First look at the parameters of callback
What does the parameter with * mean?
Here's a look at the code:
Def calc (*numbers): sum = 0for i in numbers:sum = sum + ireturn Sumresult = Calc (All-in-all) temp = Calc () Print Resultprinta Temp
The result of the execution is:
In other words, the parameter with * represents a variable number of parameters (length can be 0). See links for specific explanations:
Http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/ 001374738449338c8a122a7f2e047899fc162f4a7205ea3000
And then the GetAttr.
The official documentation for GETATTR () is interpreted as:
GetAttr (object, name[, default])
Return the value of the Namedattribute of object. Name must is a string. The If the string is the name of the one ofthe object ' s attributes, and the result is the value of the. 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 has a property named name (name must be a string), the property is returned. If the attribute is not in the object, a Attributeerror error is thrown if the default parameter is provided in GetAttr and the supplied default is returned.
Look at the following example:
Lass Test:def Print_hello (self):p rint ' func print_hello work ' A = Test () b = getattr (A, ' Print_hello ', None) print bb () b = Geta TTR (A, ' Nofunc ', None) print B
The result of the execution is:
The first time you use the GetAttr function, B tries to get the Print_hello function in a, and from the printout we can see that we have called B after the success.
When using the GetAttr function for the second time, B attempts to get the Nofunc function in a, which shows that there is no function named Nonefunc in a, so the value of B is None
Then look at the callable function.
Look at the picture
It is clear that if the argument in callable is the function name, it returns TRUE, otherwise false is returned. You can also see that func () executes once on the command line. The execution of a function occurs in the process of a = func ()
In summary, the function callback (Self,prefix, name, *args) functions as:
In an object that uses the callback function, query whether it has a function named ' Prefix+name '. If this function is available, the function is called and the execution result of the function is returned. (The function is named Prefix+name and the parameter is *args). Try 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 ', ' all-in-a-, ') Print BB = A.callback (' print ', ' sum ') print B
The result of the execution is:
Python Basic Tutorial Notes-item 1-Instant tag-day3