Python Getting Started Tutorial (iii)

Source: Internet
Author: User
Tags python list


Supposedly, this is the end of the story. There may be a bit of terminology, but most of it is the name. The important concept " class " is described under the second heading.


Functional Programming (abbreviation: FP)

  If you are interested, you can see the scheme Video tutorial (SICP public Class) (scheme is a kind of Lisp), you can also directly see the SICP translation. I just "know it".

language can be thought of as a "functional programming style" if it has one of the following characteristics (if the code uses these features, it also says that the code "has a functional programming style"):

1: The functions and the Equals, after all, are represented by symbols. If you have a good understanding of the function in high school (test: What does f (x) mean? Why do you write this? ), it's easy to see what the following lines of code are doing:

def f (x): Return 2*XG = Fprint (g (3))

2: lambda expression allowed. This slowly will understand, meaning that the essence of the function in the content, not the name, so the operation can not be named, directly with the content of the expression. The main purpose of this is that some functions (such as map) require you to pass in a function as a parameter, and you can pass it on without a name (defined by definition must be named). I have encountered the use of lambda mandatory reference in TK, uh ... Then go online to check.

3: You can define functions within a function, which you can do in Python, but the authorities do not want to do so. Something inside the function is hidden from the outside, so if you define a B function inside a function, you can use the B function (the same as the variable) only within a. But Python, as an explanatory language, is a waste of every invocation of a function that executes the inner statement (including the Def) all over again. We simply want to say that the "B function can only be quoted by a", but it does not seem worthwhile to bear such consequences. But side-by-side, seemingly no hierarchy ... (This is especially important for obsessive-compulsive disorder)


Object Oriented (abbreviation: OOP)

is also a style, and the FP does not conflict, and the FP also has the intersection. As the name implies, is the object of concern. Speaking of a certain language object-oriented, it is mainly said that there is a "class" concept. Object-oriented is also referred to as "the process of thinking close to people", people are object-oriented (can not understand this argument first).

... the Python class is really not very good to speak of. For example, to store a point, you can use a dictionary (better than the tuple, because it doesn't need a sequence and the component has a name), like this:

P1 = {' x ': 3, ' Y ': 4} #表示横坐标为3, dot print with ordinate 4 (

The class is similar to this, so that when I don't know the class, I use a dictionary of the (simple) class in a Pygame program. If you use a class, you can achieve this effect: (omit the steps to define the class, so do not try to run)

P1 = point (3,4) print (p1.x) print (P1.Y)

If you need several points, then this is certainly much better than the dictionary. Or if you encounter this situation:

A = people (' Jack ', 7) print (a.name) print (a.age)

It's at least as elegant as it looks. Here, people and point are "classes", P1 and A are "instances" of the corresponding classes, and the creation of instances is also known as "instantiation". I now make up the definition part, it should be easy to understand, at least for part ...

Class Point:def __init__ (self,a,b): self.x = Aself.y = Bclass people:def __init__ (self,a,b): Self.name = Aself.age = b

I didn't know what self was at first, and the tutorial said, "You can change self to anything." You'll want to write this code, or you can understand it if it is-(you know that INIT is the abbreviation for "definition" in English)

Class Point:def Init (A, B): x = ay = Bclass people:def init (b): name = Aage = b

Not be frank, if not python, such as C #, then this is the case, and there is no self. You can completely think of self as a device, use your habits first, and then listen to my explanations (I'm serious).

The following two pieces of code do exactly the same thing:

Class People:def __init__ (self,a,b): self.name = Aself.age = BX = people (' Jack ', 7)
Class PEOPLE:PASSX = object.__new__ (people) X.name = ' Jack ' x.age = 7

First, the name and age appear to be "intrinsic properties", but there is no such thing. By default, a class in Python can add any property "at any time". You can add a X.hello = 1 after the preceding (any) code snippet, and then Hello is also the attribute of X. For the second piece of code, the first line is obviously "create a class called people, but no content", the second line is to create a people object, but obviously it has no properties, after all, the definition of the class when nothing is written, the remaining two lines to the object appended two properties. In fact, you might have thought that creating and initializing is two procedures, creating an empty object, and initializing it to add a property. And what attributes are added is written in the initialization function. Self you should know a little bit about it, if I change it again-(the double underscore indicates that the function is official, it's more special, it's obviously initialized)

Class People:def self.__init__ (b): Self.name = Aself.age = b

If it is written in this way, it is "almost what it means", and changing self to x is the initialization of X. In fact, there is a heavy insider, that is to use a point to express the property is also to look good, the real situation is this:

X.__init__ (A, b) #其实是下一行的缩写, legend has it that Python would really treat type (x) exactly the same way. __init__ (x,a,b) #type函数返回x的类, equivalent to the next line people.__init__ (X,A,B)

Since Python internally has __init__ as three parameters, it is reasonable to write three parameters when we define it. Think: If you don't write self, how do you think Python would mistakenly "try" to run your stuff?

The simple mode of people (' Jack ', 7), is actually to make an empty object, and then (do not let you see the ground) executed the x.__init__ (' Jack ', 7). Please note! The only special thing about this function is that it is called when an object is created with a normal method. You can write anything in it, including print, like a normal function--not a method. If you write an identical function without a class, you can't use "." To invoke, the other things are exactly the same. Even if you can create it, continue calling __init__, and write the x.__init__ (' Jack ', 7) Many times (you can use it to restore it after you've modified it), but it doesn't matter if Python executes it once when it's created. You can write as many methods as you want. The consequence of not writing self is that you can still invoke it with People.f (A, B), and the principle is just the process.


Programming Style/Paradigm

Refer to the colon class-programming paradigm and OOP ideas, but be aware that Python does not have a variable type, resulting in no parameter type, so it is "generic" in itself; everything in Python is a pointer (no variable type is caused), a = 1 means create a 1 somewhere, and then point A to there-- b = A, which means to copy the position of a storage (here is the 1 position) to B, the parameters of the function are essentially these things, but these addresses are generally invisible and do not need to be visible, and therefore ("Do not let you see any memory-related things"), Python does not own the array, The Python list is consistent with the Lisp chain, and so on. Python seldom cares about the underlying algorithm, and Python makes it easier to write out the program structure.


Iterators

Try a = range (100) after print (a) and you will find very strange results. But list (a) is a listing from 0 to 99, and a is not completely useless and can be replaced by a for I in a range (100). In the older Python version, range (100) is equal to the 0-99 list, and a list is displayed if printed. But just to repeat 100 times, there is absolutely no need to first create such a long list, only need to count on it, this is the iterator. It looks like a list, but does not store each item, but calculates the next item at a time. This is a class of objects that are specifically used for iterations, which can be converted to lists using the list (), or they can be traversed directly with a for loop. In addition to the objects returned by range, this property is also true of map functions. If you want a list and you get something strange (more than one), it's possible that this thing has two properties and is an iterator. Iterators are called "very Python-style usages."


Decorator (decorator)

There is an interesting function, its input is a function, the output is a function. This function can be used as a decorator, the decorator has a special use, you can use a def on a line to write @ followed by the modifier name, you can pass that function to the decorator, and the return of the new function ("Modified function") to overwrite the original function. Like this example:

def h (func):d EF func2 (x): R = func (x) print ("be called:", func) return Rreturn func2@hdef F (x): Return 2*xa = f (3) + 1

In addition to self-search @property, but also to answer this link to explain the @staticmethod and @classmethod, I am the answer under the comments. (I feel I'm right ...) )


Built-in functions

Is Python's own function, such as print,input and so on.

(Patient view)

Python built-in functions | Beginner's Tutorial


Finally, you should know that Python developers leave us some advice, though not mandatory--(run the following code)

Import this

Finish

Python Getting Started Tutorial (iii)

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.