Usage of "Python" self literacy

Source: Internet
Author: User
Tags class definition

In Python, we have two important concepts: class and instance

For example: We in real life is a class, instance is specific to a certain man (Zhang San, John Doe, etc.)

1. Class: Define the Human class

class People (object):p

2. Example: Creating an instance is implemented by the class name + ()

People1 = People ()

3. Class is like a template, we now add some properties to this template: Age,name, using the built-in method __init__ method

class People (object):def __init__ (self,name, age): self. name nameSelf.age = Age

Note: The first parameter of the ①__init__ method is always self, which means that the created class instance itself, within __init__, can bind various properties to self,self and point to the created instance itself ② the __init__ method cannot pass in the empty argument, must pass in with the The __init__ method matches the parameters, but self does not need to be passed in

#-*-coding:utf-8-*- class People (object):def __init__ (self,name, age): self. name nameself.age = Agepeople1 = People (' Jack ', ')print(people1.  Name)#运行结果: Jackprint(people1.age)#运行结果:

The self here refers to the class itself, Self.name is the class people property variable, is people all. Name is the external parameter, not the people. Self.name = name means assigning external arguments to people's own property variables Self.name

4. when defining a function in a class, the first argument is always the instance variable self of the class itself, which is not required when passing arguments

5. The class instance itself has this data, so to access this data, there is no need to access from outside, directly in the class definition of access to data functions, so that the data can be "encapsulated" up

#-*-coding:utf-8-*- class People (object):def __init__ (self,name, age): self. name nameSelf.age = Agedef -Print_age (self):print("%s:%s"% (self).  Name, self.age)) People1 = people (' Jack ', ') people1.print_age ()#运行结果: jack:23
The logic is encapsulated, it's relatively easy to call, but it doesn't know the details of the internal implementation.

6. If you want internal properties to not be externally accessed, then only two underscores are required, which becomes a private variable that can only be accessed internally and cannot be accessed externally.

#-*-coding:utf-8-*- class People (object):def __init__ (self,namenameSelf.__age = Agedef print_age (self): Print ("%s:%s"% (self.__name,self.__age)) People1 = People (' Jack ', $) people1.print_age ()#运行结果: jack:23
Try using external access
#-*-coding:utf-8-*- class People (object):def __init__ (self,namenameSelf.__age = Agedef print_age (self): Print ("%s:%s"% (self.__name,self.__age)) People1 = People (' Jack ', ' people1 '). name #报错: ' People ' object has no attribute ' name 'people1.__name#报错: ' People ' object has no attribute ' __name ' 

Usage of "Python" self literacy

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.