"Mid Valley Education Python Video Tutorial" is a beginner's guide to Python development, which will introduce the features and scope of Python language, Python's basic data types, conditional judgments and loops, functions, and Python-specific slicing and list generation. Hopefully this Python tutorial will get you started quickly and write a simple Python program.
Course Play Address: http://www.php.cn/course/501.html
The teacher's lecture style:
Teachers teach in a clear, well-organized, layered analysis, interlocking, rigorous argumentation, structural rigorous, with the logical power of thinking to attract students ' attention, with reason to control the course of classroom teaching. By listening to the teaching of teachers, students not only learn knowledge, but also receive the training of thinking, but also by the teachers rigorous study attitude of edification and infection
The difficulty in this video is object-oriented-classes and objects:
Everything in Python is an object. class provides a mechanism for creating new types of objects. In this tutorial, we don't talk about the basics of class and object-oriented, but focus on a better understanding of Python's object-oriented programming. Let's say we use the new style Python class, which inherits from the object parent class.
Defining classes
The class statement can define a series of properties, variables, methods, and they are shared by instance objects of the class. A simple class definition is given below:
Class Account (object): num_accounts = 0 def __init__ (self, Name, balance): self.name = name Self.balance = Balance account.num_accounts + 1 def del_account (self): account.num_accounts-= 1 def Deposit (Self, AMT): self.balance = Self.balance + amt def withdraw (self, AMT): self.balance = self.balance -AMT def inquiry (self): return self.balance
The class definition introduces the following new objects:
Class object
Instance Object
Method Object
Class object
When a class definition is encountered during program execution, a new namespace is created, and the namespace contains the name bindings for all class variables and method definitions. Note that the namespace does not create a new local scope that the class method can use, so accessing the variable in the method requires a fully qualified name. The account class in the previous section demonstrates this feature, and the method that attempts to access the num_of_accounts variable needs to use the fully qualified name account.num_of_accounts, otherwise, if the fully qualified name is not used in the __init__ method, The following error is raised:
Class Account (object): num_accounts = 0 def __init__ (self, Name, balance): self.name = name self.balance = ba Lance Num_accounts + = 1 def del_account (self): account.num_accounts-= 1 def deposit (Self, AMT): Self.balance = Self.balance + amt def withdraw (self, AMT): self.balance = Self.balance-amt def inquiry (sel f): return self.balance >>> acct = account (' Obi ', ten) Traceback (most recent call last): File "Python", Line 1, in <module> File "Python", line 9, in __init__unboundlocalerror:local variable ' num_accounts ' referenced b Efore Assignment
At the end of the class definition execution, a class object is created. The scope that is valid before entering the class definition is now restored, and the class object is bound to the class name of the class definition header.
First off the topic, you might ask, if the class being created is an object, then what is the class of the class object? Consistent with the Python philosophy that everything is an object, the class object does have a class, the type class in the new Python style class.
>>> type (account) <class ' type ' >
To make you more confused, the type of account type is. The type class is a meta class that is used to create other classes that we'll cover later in the tutorial.
Class objects support property references and instantiation. The Obj.name property is referenced by a standard point syntax, that is, the object is followed by a period and then the property name:. A valid property name is any variable and method name that occurs in the class namespace after the class object is created. For example:
>>> account.num_accounts>>> 0>>> account.deposit>>> <unbound method Account.deposit>
Class instantiation uses function notation. Instantiation will invoke class objects like normal functions without arguments, as in the account class in the following article:
>>> account ()
After the class object is instantiated, the instance object is returned, and if the __init__ method is defined in the class, it is called, and the instance object passes past as the first argument. This method takes a user-defined initialization process, such as initialization of an instance variable. For example, account name and balance are set, and the number of instance objects increases by 1.
Instance Object
If the class object is a cookie cutter, the cookie is the result of instantiating the class object. All valid operations on an instance object are references to properties, data, and method objects.
Method Object
Method objects are similar to function objects. If X is an instance of the account class, X.deposit is an example of a method object. There is an additional parameter in the method definition, self. Self points to the class instance. Why do we need to pass the instance as a parameter to the method? A method call can best describe:
>>> x = account () >>> X.inquiry () 10
What happens when an instance method is called? You should notice that the X.inquiry () call has no arguments, although the method definition contains the self parameter. So what exactly happened to this parameter?
The
is special because the object that the method acts on is passed in as the first parameter of the function. In our case, the call to X.inquiry () is equivalent to ACCOUNT.F (x). In general, the method of invoking the n parameter is equivalent to inserting the action object of the method into the first parameter position.