Python Learning note 12-Class

Source: Internet
Author: User

Typical classes and methods of invocation:

#!/usr/bin/env Python#Coding=utf-8__metaclass__= Type#New ClassclassPerson:#Create Class    def __init__(self, name):#initialization functionSelf.name =Name
defGetName (self):#methods (functions) in a class returnSelf.name
defcolor (self, color):Print "%s is%s"%(self.name, color)
Girl= Person ('Wangguniang')#instantiation ofName = Girl.getname ()#Call Method (function)Print "The person's name is:", Namegirl.color (" White")#Call Method (function)Print "------"PrintGirl.name#properties of the instance

Operation Result:

The property data for self must not necessarily be passed in by a parameter, or it can be set in the constructor itself

#/bin/env/python#Coding:utf-8__metaclass__=typeclassPerson :def __init__(Self, name,general="male"): Self.name=name Self.email="[email protected]"self.general=GeneralInfo= Person ("Keven")Print "info.name=", Info.namePrint "info.email=", Info.emailPrint "info.general=", info.general

Operation Result:

Python Learning note 12-Class

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.