What is a Python class property? How do I access properties with Python?

Source: Internet
Author: User
Tags access properties
Python class Propertiesis a function of the language itself, this article will explain through the example of what is Python class letters,and how to use Python access properties.

A. The Python class properties are:

1.__DICT__: Properties of the Class (contains a dictionary, consisting of the data properties of the Class)

2.__DOC__: Document string for Class

3.__NAME__: Class name

4.__MODULE__: The module where the class definition resides (the full name of the class is ' __main__.classname ', if the class is in an import module mymod, then classname.__module__ equals Mymod)

5.__BASES__: All parent classes of a class make up elements that contain a tuple of all the parent classes

two. So how do you go about accessing properties with Python ?

We can use the dot number. To access the properties of the object. Access the class variable using the name of the following class:

Emp1.displayemployee () Emp2.displayemployee () print "Total Employee%d"% Employee.empcount

Contact one, two, let's give an example

#!/usr/bin/python#-*-Coding:utf-8-*-class Employee:   ' base class for all employees '   empcount = 0    def __init__ (self, name, Sala RY):      self.name = name      self.salary = Salary      Employee.empcount + =      1 def displaycount (self):        print "  Total Employee%d '% employee.empcount    def displayemployee (self):           print "Name:", Self.name,  ", Salary:", Self.salary print "employee.__doc__:", employee.__doc__ print "employee.__name__:", employee.__name__ print "Employee. __module__: ", employee.__module__ print" employee.__bases__: ", employee.__bases__ print" employee.__dict__: ", employee.__dict__

Execute the above code to output the result as follows:

employee.__doc__: base class for all employees employee.__name__: employeeemployee.__module__: __ Main__employee.__bases__: () employee.__dict__: {' __module__ ': ' __main__ ', ' displaycount ': <function displayCount at 0x10a939c80>, ' Empcount ': 0, ' displayemployee ': <function displayemployee at 0x10a93caa0> ' __doc__ ': ' \xe6\ X89\x80\xe6\x9c\x89\xe5\x91\x98\xe5\xb7\xa5\xe7\x9a\x84\xe5\x9f\xba\xe7\xb1\xbb ', ' __init__ ': <function __init __ at 0x10a939578>} 
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.