Defining instance methods in Python

Source: Internet
Author: User

Please add a private attribute __score to the person class, represent fractions, and then add an instance method Get_grade (), which can return a-excellent, B-pass, C-fail three files according to the value of __score.

class Person (object):

def __init__ (self, Name, score):
Self.name=name
Self.__score=score
#good =a
#fine =b
#nonono =c

def get_grade (self):
If self.__score>=90:
Return ' A '
if (self.__score<90) and (self.__score>=60):
Return ' B '
If self.__score<60:
Return ' C '

P1 = person (' Bob ', 40)
P2 = person (' Alice ', 65)
P3 = person (' Tim ', 48)

Print (P1.get_grade ())
Print (P2.get_grade ())
Print (P3.get_grade ())

Defining instance methods in Python

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.