Python learning Day 7 object-oriented class and instance access restrictions, pythonday

Source: Internet
Author: User

Python learning Day 7 object-oriented class and instance access restrictions, pythonday

Object-Oriented Programming

Object Oriented Programming (OOP) is a Programming idea. OOP regards objects as the basic unit of a program. An object contains functions for data and operation data.

Process-oriented programming treats a computer program as a collection of commands, that is, the sequential execution of a group of functions. To simplify the program design, process-oriented functions are further split into subfunctions, that is, large functions are cut into small functions to reduce the complexity of the system.

Object-Oriented Programming treats computer programs as a collection of objects, and each object can receive and process messages sent from other objects, the execution of a computer program is the transmission of a series of messages between objects.

In Python, all data types can be regarded as objects, and you can also customize objects. The custom object data type is the concept of Class in the object-oriented model.

Class and instance

Classes are abstract templates, such as Student classes, and instances are specific "objects" created based on classes. Each object has the same method, but its data may be different.

Define a class by using the class keyword

Class Student (object): # define a class by using the class keyword

 

Pass

>>> Bart = Student ()

>>> Bart

<__Main _. Student object at 0x10a67a590>

>>> Student

<Class '_ main _. Student'>

>>> Class Student (object ):

Def _ init _ (self, name, score ):

Self. name = name

Self. score = score

Compared with a common function, the function defined in the class is only a little different, that is, the first parameter is always the instance variable self, and you do not need to pass this parameter when calling.

Data encapsulation
>>> defprint_score(std):
...     print'%s: %s' % (std.name, std.score)
...
>>> print_score(bart)
Bart Simpson: 59
classStudent(object):
 
    def__init__(self, name, score):
        self.name = name
        self.score = score
 
    defprint_score(self):
        print'%s: %s' % (self.name, self.score)

A class is a template for creating an instance, and an instance is a specific object. The data of each instance is independent of each other and does not affect each other;

The method is the function bound to the instance. Unlike the common function, the method can directly access the instance data;

By calling a method on an instance, we directly manipulate the data inside the object, but do not need to know the implementation details inside the method.

Different from static languages, Python allows you to bind any data to instance variables. That is to say, for both instance variables, although they are different instances of the same class, their variable names may be different.

Access Restrictions

In the Class, attributes and methods can be available, while external code can directly call the method of instance variables to operate data, thus hiding the internal complex logic. If you want to prevent internal properties from being accessed externally, you can add two underlines __before the attribute name. In Python, if the instance variable name starts, it becomes a private variable, which can only be accessed internally and cannot be accessed externally.

ClassStudent (object ):

Def _ init _ (self, name, score ):

Self. _ name = name

Self. _ score = score

Defprint_score (self ):

Print '% s: % s' % (self. _ name, self. _ score)

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.