Python Object-oriented: class, scope

Source: Internet
Author: User

Classes (Class) and instances (Instance)

Defining a class is through the Class keyword:
Class Student (object):
Pass

Class name is followed by classes, i.e. student
followed by (object), indicating which class the class inherits from.

Create an instance of student based on the student class, which is implemented by the class name + ():
>>> Bart = Student ()

Class Student (object):

def __init__ (self, Name, score):
self.name = name
Self.score = Score
The first argument of the __init__ method is always self, which represents 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 parameter that matches the __init__ method, but self does not need to pass
>>> bart = Student (' Bart Simpson ', ()
>>> Bart.name
' Bart Simpson '
>>> Bart.score
-

Defines a method, except that the first argument is self, and the other is the same as a normal function

Python allows you to bind any data to an instance variable, that is, for two instance variables, although they are different instances of the same class, the variable names you have may be different

Limit
>>> bart = Student (' Bart Simpson ', ()
>>> Bart.score
-
>>> Bart.score =
>>> Bart.score
About
If you want internal properties to be inaccessible externally, you can add two underscores to the name of the property
in Python, the variable name of an instance begins with __, and becomes a private variable (private) that can only be accessed internally and cannot be accessed externally .
class Student (object):

def __init__ (self, Name, score):
Self.__name = Name
Self.__score = Score

External code to get name and score
To add get_name and Get_score methods to the Student class:
Class Student (object):
...

def get_name (self):
Return Self.__name

def get_score (self):
Return Self.__score
External Code Modification Score
You can add the Set_score method to the student class again:

Class Student (object):
...

def set_score (self, score):
Self.__score = Score

An instance variable name that starts with an underscore, such as _name, can be accessed outside of an instance variable, but, as you see in the rules, when you look at such a variable, it means, "although I can be accessed, but please treat me as a private variable, do not feel free to access".

A special variable that starts with a double underscore and ends with a double underscore, is a special variable that can be accessed directly, not a private variable

The __name cannot be accessed directly because the Python interpreter has changed the __name variable to _student__name, so the __name variable can still be accessed through _student__name:

>>> Bart._student__name
' Bart Simpson '

Python Object-oriented: class, scope

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.