Home  >  Article  >  Backend Development  >  Zhonggu Education Python video tutorial resource recommendation

Zhonggu Education Python video tutorial resource recommendation

黄舟
黄舟Original
2017-09-01 14:22:051661browse

"Zhonggu Education Python Video Tutorial" is an introductory tutorial for Python development. It will introduce the characteristics and scope of application of the Python language, Python's basic data types, conditional judgments and loops, functions, and Python's unique slices and lists. Generative expression. Hopefully this python tutorial will get you started quickly and write simple Python programs.

Zhonggu Education Python video tutorial resource recommendation

Course playback address: http://www.php.cn/course/501.html

The teacher’s teaching style:

The teacher’s lectures are simple, clear, layer-by-layer analysis, interlocking, rigorous argumentation, rigorous structure, and use the logical power of thinking to attract students’ attention Strength, use reason to control the classroom teaching process. By listening to the teacher's lectures, students not only learn knowledge, but also receive thinking training, and are also influenced and influenced by the teacher's rigorous academic attitude

The more difficult point in this video is object-oriented-class and object Now:

Everything in Python is an object. Classes provide a mechanism for creating new types of objects. In this tutorial, we will not talk about the basic knowledge of classes and object-oriented programming, but focus on better understanding Python object-oriented programming. Suppose we use new style Python classes, which inherit from the object parent class.
Define class

The class statement can define a series of attributes, variables, and methods, which are shared by instance objects of this 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 will be created. The namespace contains the name bindings of all class variables and method definitions. Certainly. Note that this namespace does not create a new local scope that class methods can use, so accessing variables within methods requires fully qualified names. The Account class in the previous section demonstrates this feature; 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 will be raised:

class Account(object):
 num_accounts = 0
  
 def __init__(self, name, balance):
  self.name = name
  self.balance = balance
  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
  
>>> acct = Account('obi', 10)
Traceback (most recent call last):
 File "python", line 1, in <module>
 File "python", line 9, in __init__
UnboundLocalError: local variable &#39;num_accounts&#39; referenced before assignment

At the end of the execution of the class definition, a class object will be created. The scope that was in effect before entering the class definition is now restored, and the class object is bound to the class name in the class definition header.

Let’s deviate from the topic first. You may ask if the class created is an object, then what is the class of the object? . Consistent with the Python philosophy that everything is an object, class objects do have a class, which is the type class in Python's new style classes.

>>> type(Account)
<class &#39;type&#39;>

To confuse you a little more, the type of Account type is type. The type class is a metaclass used to create other classes, which we will introduce later in the tutorial.

Class objects support attribute reference and instantiation. Properties are referenced via standard dot syntax, i.e. object followed by a period, then the property name: obj.name. Valid property names are all variable and method names that appear 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 call the class object without parameters like a normal function, such as the Account class below:

>>> Account()

After the class object is instantiated, the instance object will be returned. If the __init__ method is defined in the class, it will be called , the instance object is passed as the first parameter. This method will perform user-defined initialization process, such as initialization of instance variables. For the Account class, for example, the account name and balance will be set, and the number of instance objects will be increased 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 instance objects are references to property, data, and method objects.
Method object

Method object is similar to function object. 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 instances as parameters to methods? Method calls best illustrate:

>>> x = Account()
>>> x.inquiry()
10

What happens when an instance method is called? You should note that x.inquiry() is called without parameters, although the method definition contains the self parameter. So what exactly is happening with this parameter?

The special thing is that the object acted upon by the method is passed as the first parameter of the function. In our example, a call to x.inquiry() is equivalent to Account.f(x). Generally, calling a method with n parameters is equivalent to inserting the method's action object into the first parameter position.

The above is the detailed content of Zhonggu Education Python video tutorial resource recommendation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn