Python Object-oriented

Source: Internet
Author: User

Properties of the class
    • Packaging
    • Inherited
    • Properties of a polymorphic definition class
    • Define directly inside a class
    • Define a class with python in the constructor
#!/usr/bin/env python# -*- coding: utf-8 -*-__author__ = ‘wangke‘__time__ = ‘2018-01-26 15:47‘class Programer(object):    hobby = ‘Play Computer!‘    def __init__(self, name, age, weight):        self.name = name        self._age = age        self.__weight = weight    def get_weight(self):        return self.__weightif __name__ == ‘__main__‘:    Programer = Programer(‘xiaoke‘,‘25‘,88) #实例化对象    print(dir(Programer))    print(Programer.__dict__) #打印从构造函数获得的属性    print(Programer.get_weight())    # print(Programer.__weight) 直接获取会出错,使用了__定义的属性相当于私有属性,但是非要访问可以通过下面的方式访问到    print(Programer._Programer__weight)

Execution results are as follows

Python Object-oriented

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.