Python: Creating an instance of a class

Source: Internet
Author: User

Class Student (object):
def __init__ (self,name= "", school= "", Grade= ""):
If not name:
Name=raw_input ("What's the student ' s name?")
If not school:
School=raw_input ("What's the student ' s school?")
If not grade:
Grade=self.get_grade ()
Self.name=name
Self.school=school
Self.grade=grade
Self.print_student ()
def get_grade (self):
While True:
Grade=raw_input ("What's the student ' s grade?[ K,1-5] ")
If Grade.lower () not in [' K ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']:
Print "I ' m sorry, but {} isn ' t valid." Format (grade)
Else
Return grade
def print_student (self):
Print "Name: {}". Format (Self.name)
Print "School: {}". Format (Self.school)
Print "Grade: {}". Format (Self.grade)

def main ():
Student1=student ()
Student2=student (name= "Harry", grade= "2", school= "Minnieville")


If __name__== "__main__":
Main ()


Note: 1. You must have the parameter object in the Created class
2. "! "To become Chinese symbol ' Hello world\xa3\xa1 ' may appear error
3. Methods in a class pass in a value that must have self, in the method body reference property
4. When calling this class, do not use the duplicate object
5.__name__, __main__, and __init__ are double-underlined
The 6.python comes with the initialization method __init__ (), which is called automatically when the class is called

Python: Creating an instance of a class

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.