Concept and use of python-Class 1

Source: Internet
Author: User

1. Class: used to describe a collection of objects that have the same properties and methods. It defines the properties and methods that are common to each object in the collection. An object is an instance of a class.

#!/usr/bin/python#-*-Coding:utf-8-*-Class Employee:#' base class for all employees 'Empcount= 0 Def__init__(Self,Name,Salary): Self.Name=NameSelf.Salary=SalaryEmployee.Empcount+= 1 DefDisplaycount(Self): Print "Total Employee%d" % Employee.EmpcountDefDisplayemployee(Self): Print("Name:", Self.Name, ", Salary:", Self.Salary# "Create first object of Employee class"Emp1= Employee("Zara", 2000) emp2 = employee ( "Manni" , 5000 emp1. () emp2. () print (  "total Employee%d"  %  Employee. Empcount)                  

Execute the above code to output the result as follows:

Name:  Zara,Salary: +-name:Manni  ,Salary: TotalEmployee2


2.
Emp1.  =7# Add an ' age ' attribute emp1.  =8# Modify the ' age ' property del emp1.  # delete ' age ' property            



    • GetAttr (obj, name[, default]): Accesses the properties of the object.
    • Hasattr (obj,name): Checks if a property exists.
    • SetAttr (Obj,name,value): Sets a property. If the property does not exist, a new property is created.
    • Delattr (obj, name): Deletes the attribute.
Hasattr(Emp1,  ' age ' )  # if the existence of the ' age ' property returns TRUE. getattr (emp1,  ' age ' )  # return the value of the ' age ' property setattr< Span class= "pun" > (emp1,  ' age ' , 8)   # Add attribute ' age ' value to 8delattr (empl  ' age ' )  # delete attribute ' age '   

Concept of the python-class and its use 1

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.