Python learning Day11 Object-oriented programming classes and instances

Source: Internet
Author: User

classStudent (object):#The class name is followed by student, the class name is usually the first word in uppercase, followed by (object), indicating which class the class inherits from. In general, if there is no suitable inheriting class, use the object class, which is the class that all classes will eventually inherit.     def __init__(Self,name,score):#by defining a special __init__ method, when the instance is created, the class's Name,score attributes are bound up, and the first parameter of the __init__ method is always self, which represents the created instance itself, so inside the __init__ method, You can bind the various properties to self, because it points to the created instance itself, and the parameters are consistent with the arguments of the function in addition to the, and can still be used with default, variable, keyword, and named keyword parameters. =name Self.score=scoredefPrint_score (self):#A function that accesses data is defined inside the student class so that the "data" is encapsulated. The functions of these encapsulated data are associated with the student class itself, which we call the method of the class, to define a method, except that the first argument is self, and the other is the same as a normal function. To invoke a method, just call it directly on the instance variable, except that self does not pass, and other parameters are passed in normally.        Print("%s:%s"%(,self.score))defGet_grade (self):#Another benefit of encapsulation is that you can add new methods to the student class, such as Get_grade, and the Get_grade method can be called directly on the instance variable without knowing the internal implementation details        ifSelf.score >= 90:            return "A"        elifSelf.score >= 60:            return "B"        Else:            return "C"Bar= Student ("Bart Simpson", 59) Bar.print_score () RET=Bar.get_grade ()Print(ret) Lisa= Student ("Lisa Simpson", 87) Lisa.age= 18Print(dir (Lisa))#using Dir () to see how a class (or variable) can be used, you can see that the Lisa instance has an older method than the bar instance because our Lisa.age = 18 statement adds a new attribute to Lisa age. Unlike static languages, 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.#[' __class__ ', ' __delattr__ ', ' __dict__ ', ' __dir__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __ge__ ', ' __getattribute__ ' ', ' __gt__ ', ' __hash__ ', ' __init__ ', ' __init_subclass__ ', ' __le__ ', ' __lt__ ', ' __module__ ', ' __ne__ ', ' __new__ ', ' __ reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' __weakref__ ', ' Age ', ' Get_grade ', ' name ', ' Print_score ', ' score ']Print(dir (bar))#[' __class__ ', ' __delattr__ ', ' __dict__ ', ' __dir__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __ge__ ', ' __getattribute__ ' ', ' __gt__ ', ' __hash__ ', ' __init__ ', ' __init_subclass__ ', ' __le__ ', ' __lt__ ', ' __module__ ', ' __ne__ ', ' __new__ ', ' __ reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' __weakref__ ', ' Get_grade ', ' name ', ' Print_score ', ' score ']

#******************************************** Access Restrictions **********************************#in Python, the variable name is similar to __xxx__, which starts with a double underscore and ends with a double underscore, which is a special variable that can be accessed directly, not a private variable .#Sometimes you see variables that start with a single underline, such as _name, which can be accessed outside of an instance variable, but, as you see in the rules, when you look at a variable like this, it means, "although I can be accessed, but please treat me as a private variable, don't feel free to access"#An instance variable that starts with a double underscore is generally not accessible directly from the outside, but it is not necessarily inaccessible from outside. 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, but it is strongly advised not to do so. Because different versions of the Python interpreter might change the __name to a different variable nameclassStudent (object):def __init__(Self,name,score): Self.__name=name self.__score=scoredefPrint_score (self):Print("%s:%s"% (self.__name, self.__score))    defSet_score (self, score):#by internal methods to modify the __score #在方法中 inside the class, you can check the parameters to avoid passing invalid arguments:        if0 <= Score <= 100: Self.__score=scoreElse:            RaiseValueError ('Bad score') Bart= Student ("Bart Simpson", 59) Bart.print_score ()#Bart simpson:59Bart.set_score (89) Bart.print_score ()#Bart simpson:89

Python learning Day11 Object-oriented programming classes and instances

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.