Basic Python Tutorial 09: Object-oriented further development

Source: Internet
Author: User

How do you get to know Python quickly? It's a matter of conversation with friends.

Python contains a lot of content, plus a variety of standard libraries, expand the library, the flower is increasingly attractive eye. I have always wanted to write a quick, easy-to-follow Python tutorial, and the words are concise and gradual, so that no background readers can start learning from the basics. I will focus on a small concept in each article and hope to be able to read it quickly in my spare time.

Small reminders

  • The tutorial will focus on the Python basics, the syntax based on Python 2.7 (I'll remind the Python 3.x that there's a change in the place to make it easier for readers to adapt to 3. X is the case). The test environment is Linux. Some packages of the standard library are not available for Windows platforms. If the program in the article doesn't work on your platform, you're welcome to discuss it.
  • I'm going to focus on the backbone of Python so that the reader can develop a concept of python in the quickest possible time.
  • The Linux command line starts with $, such as $ls, $python
  • The python command line starts with >>>, such as >>>print ' Hello world! '
  • Comments begin with #

Suggestions

  • Tap the commands in the tutorial to see the effect in Python.
  • After you've seen the tutorials, you can do some exercises.
  • Participate in the discussion of the article comment area, can accumulate the experience better.

Tutorial Content

We are familiar with the basic concepts of objects and classes. We will further expand so that we can actually use objects and classes.

Calling additional information for the class

When defining a method, you must have the self parameter. This parameter represents an object. Object has all the properties of a class, we can invoke the Class property through self.

Class Human (object):

Laugh = ' hahahaha '

def show_laugh (self):

Print Self.laugh

def laugh_100th (self):

For I in range (100):

Self.show_laugh ()

Li_lei = Human ()

li_lei.laugh_100th ()

Here is a class attribute laugh. In Method Show_laugh (), the value of the property is called through Self.laugh.

Other methods can also be called in the same way. Method Show_laugh (), which is called in Method laugh_100th ().

You can modify class property values through an object. But it's dangerous. The Class property is shared by all objects of the same class and its subclasses. Changing the value of a class property affects all objects.

__init__ () method

__init__ () is a special approach (special method). Python has some special methods. Python treats them in a special way. The special method is characterized by two underscores before and after a name.

If you define the __init__ () method in your class, Python automatically calls this method when you create the object. This process is also called initialization.

Class Happybird (Bird):

def __init__ (self,more_words):

print ' We are happy birds ', more_words

Summer = Happybird (' happy,happy! ')

The bird class is inherited here, and its definition is shown in the previous speech.

On-screen printing:

We are happy birds. happy,happy!

We see that although we just created the summer object, the __init__ () method is called automatically. The last line of the statement (Summer = Happybird ...) The object is created first and then executed:

summer.__init__ (More_words)

The ' happy,happy! ' was passed to the __init__ () parameter more_words

The nature of the object

We've talked about many properties, but these are properties of the class. All objects that belong to the class share these properties. For example, birds have feathers and chickens can't fly.

In some cases, we define the nature of the object, which is used to record special information about the object. For example, people in this class. Sex is a person's nature, not all human beings are men, or all are women. The value of this property varies with the object. Li Lei is a human object, gender is male, Han Meimei is also a human object, gender is female.

When you define a method for a class, you must pass a self argument. This parameter refers to an object of the class. We can modify the nature of an object by manipulating self. For example, to create a new object with a class, that is, li_lei in the example below, then Li_lei is represented by self. By assigning to Self.attribute, we add some properties to the object of Li_lei, such as gender. Self is passed to each method. Inside a method, you can query or modify the nature of an object by referencing Self.attribute.

In this way, in addition to the class attributes, each object is added to the nature of their own characteristics, so as to describe a diverse world.

Class Human (object):

def __init__ (self, Input_gender):

Self.gender = Input_gender

def printgender (self):

Print Self.gender

Li_lei = Human (' Male ') # here, ' Male ' is passed as a parameter to the __init__ () method for the Input_gender variable.

Print Li_lei.gender

Li_lei.printgender ()

In initialization, the parameters are Input_gender and assigned to the properties of the object, i.e. Self.gender.

Li_lei has the object nature gender. Gender is not a class property. After creating the Li_lei object, Python uses the object nature of Li_lei.gender to store unique information that belongs to the object Li_lei.

The nature of an object can also be called by other methods, similar to the invocation of a class property, as in the Printgender () method.

Summarize

Invoking a class property through self

__init__ (): Automatically executes when an object is created

The difference between a class property and the nature of an object

http://www.biyinjishi.com/products/a65-b6535/
Http://www.biyinjishi.com/products/a65-b6550/
http://www.biyinjishi.com/products/a65-b6560/
http://www.biyinjishi.com/products/a65-b6570/
http://www.biyinjishi.com/products/a65-b6580/
http://www.biyinjishi.com/products/a65-b6599/

Basic Python Tutorial 09: Object-oriented further development

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.