Python Basic Learning Log reflection of the Day7-class

Source: Internet
Author: User

1) The reflection function in Python is provided by the following four built-in functions: Hasattr, GetAttr, SetAttr, Delattr,

Four functions are used for internal execution of objects: Check for a member, get a member, set a member, delete a member.

#-*-coding:utf-8-*-__author__='Shisanjun'classFoo (object):def __init__(self): Self.name="s"    deffunc (self):return 'func'obj=Foo ()#Check if a member is includedPrint("----Whether there are members-----")Print(Hasattr (obj,"name"))Print(Hasattr (obj,"func"))#Get MembersPrint("----GET Members-----")Print(GetAttr (obj,"name"))Print(GetAttr (obj,"func")())#memory address of Func for GetAttr (obj, "func") instance#Set memberPrint("----SET member-----")defShow (num):returnNum+1setattr (obj," Age", 18) setattr (obj,"Show", 3)Print(GetAttr (obj," Age"))Print(GetAttr (obj,"Show")) delattr (obj," Age") delattr (obj,'Show')Print("----Whether there are members-----")Print(Hasattr (obj," Age"))Print(Hasattr (obj,"Show"))"""----If there are members-----truetrue----GET members-----SFUNC----SET member-----183----Whether there are members-----Falsefalse"""

2) Other methods to access members of the class

class Foo (object):     def __init__ (self):         ' Alex '    def func (self):         return ' func ' # do not allow obj.nameobj = Foo ()print obj.__dict__['name  ']

Python Basic Learning Log reflection of class day7-

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.