Python object-oriented class two and instance

Source: Internet
Author: User

I. Classes and instances

Object-oriented concepts are classes (class) and instances (Instance), and it is important to keep in mind that classes are abstract templates, such as student classes, and instances are specific "objects" that are created from classes, each with the same method, but the data may be different for each object.

1 class Student (object): 2 ...     Pass 3

In Python, the definition of a class is by keyword, followed by the class name, that is, the class class Student class name is usually the beginning of the word, followed by the class is the class (object) from which to inherit, the concept of inheritance we will say later, usually, if there is no appropriate inheritance class, Just use the object class, which is the class that all classes will eventually inherit.

Once you have defined the Student class, you can Student create Student an instance from the class by using the class name + () to create the instance:

1 >>> st1 = Student ()2 >>> st13 <__main__. Student object at 0x1014b2320>4 >>> Student5 <class' __main__. Student'>

As you can see, the variable st1 points to an Student instance, followed by a 0x1014b2320 memory address, each object has a different address, and Student itself is a class.

Second, instance properties

You can freely bind a property to an instance variable, for example, bind an attribute to an instance st1 and an age name property:

1 ' Jack ' 2 >>> st1.age =3 >>> st1.name4'Jack'  5 >>> st1.age6 28

Three, __init__ () function

Because a class can act as a template, you can force a number of attributes that we think must be bound to be filled in when creating an instance. By defining a special __init__ method, when you create an instance, you bind the name attributes of age and so on:

1>>>classStudent (object):2...def __init__(self, Name, age):3... =name4... Self.age = Age5 ... 6>>> st1 = Student ('Mick', 20)7>>>St1.name8 'Mick'9>>>St1.ageTen20

Notice that the __init__ first parameter of the method is always the one that self represents the created instance itself, so within the __init__ method, you can bind various properties to it self , because self it points to the created instance itself.

With the __init__ method, when you create an instance, you cannot pass in an empty argument, you must pass in a __init__ parameter that matches the method, but it self does not need to be passed, and the Python interpreter will pass in the instance variable itself.

A function defined in a class is only a little different than a normal function, that is, the first argument is always an instance variable, self and when called, it is not passed. In addition, there is no difference between a class method and a normal function, so you can still use default parameters, variable arguments, keyword arguments, and named keyword parameters.

Iv. Data Encapsulation

A Student function that accesses data is defined inside the class, so that the "data" is encapsulated. The functions of these encapsulated data are Student associated with the class itself, and we call it the method of the class:

 1  class   Student (object):  2  3  def  __init__   (self, name, score):  4 = name  5  self.score = score  6  7  def   Print_score (self): Span>8  print  ( '  
1 >>>st1 = Student ('Jack', 2) >>>  St1.print_score ()3 jack:59

class methods encapsulate data and logic, just call it and don't know how to implement it.

The use of classes makes it easy to add new methods.

1 classStudent (object):2     ...3 4     defGet_grade (self):5         ifSelf.score >= 90:6             return 'A'7         elifSelf.score >= 60:8             return 'B'9         Else:Ten             return 'C'

Python object-oriented class two and instance

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: 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.