Python Object-oriented fields

Source: Internet
Author: User

Class Members: Fields, methods, properties

property is preceded by a function method with an adorner, forged into a field, called without parentheses ()

The purpose is to forge a method into a field, and the property is one of the methods, and the method can be modified to

field to access a method is a series of actions that return a result, the Access property is equivalent to its own

A trait, not an action.

Vim day7-5.py

#!/usr/bin/python#-*-coding:utf-8-*-class Person (object):    def __init__ (self,name,age):        self.name = name        Self.age = Age    def func (self):        return ' 123 '    def proporty ():        pass    @property    def att (self ):        return ' 123 ' obj = person (' 1 ', 2) print Obj.nameprint obj.func () print Obj.att

Fields divided into normal fields and static fields

Vim day7-6.py

#!/usr/bin/python#-*-coding:utf-8-*-class Province (object):    country = "China"            #静态字段, there is memory inside    def __init__ ( Self,name):        self.name = name        #普通字段, stored inside the object shanxi = province ("shanxi") Shangdong = Province ("shandong") Henan = Province (" Henan ") Print ID (shanxi.country) Print ID (shangdong.country) print Shanxi.name

Python Object-oriented fields

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.