python-Object-oriented advanced

Source: Internet
Author: User

First, what is reflection

The concept of reflection, first proposed by Smith in 1982, is a capability (introspection) that a program can access, detect, and modify its own state or behavior. The proposal of this concept soon triggered the research on the application of reflectivity in Computer science field. It is first used in the field of programming language design, and has achieved achievements in Lisp and object-oriented.

二、四个 functions that can implement introspection (the following methods apply to classes and objects)

1, hasattr (object,name)

Determine if there is a method or property in the object that has a name string

2, GetAttr (object, name, Default=none)

3, SetAttr (x, y, v)

4, Delattr (x, y)

Example:

1 classBlackmedium:2Feature='Ugly'3     def __init__(SELF,NAME,ADDR):4Self.name=name5Self.addr=Addr6 7     defSell_house (self):8         Print('%s Black intermediary to sell the house, stupid to buy it, but who can prove that they are not stupid'%self.name)9     defRent_house (self):Ten         Print('%s Black intermediary rent a house, stupid only rent it'%self.name) One  AB1=blackmedium ('million into place','Huilongguan Tian Lu Yuan') -  - #detect if a property is included the Print(Hasattr (B1,'name')) - Print(Hasattr (B1,'Sell_house')) -  - #Get Properties +N=getattr (B1,'name') - Print(n) +Func=getattr (B1,'Rent_house') A func () at  - #GetAttr (B1, ' aaaaaaaa ') #报错 - Print(GetAttr (B1,'aaaaaaaa','It doesn't exist.')) -  - #Setting Properties -SetAttr (B1,'SB', True) inSetAttr (B1,'Show_name',Lambdaself:self.name+'SB') - Print(B1.__dict__) to Print(B1.show_name (B1)) +  - #Delete Property theDelattr (B1,'Addr') *Delattr (B1,'Show_name') $ #delattr (B1, ' show_name111 ') #不存在, then errorPanax Notoginseng  - Print(B1.__dict__)

Execution Result:

1 True2 True3 million into place4 million into the black intermediary house, stupid to rent it5 It doesn't exist.6{'name':'million into place','Show_name': <function <Lambda> at 0x01142f60>'SB': True,'Addr':'Huilongguan Tian Lu Yuan'}7Million Place SB

Third, why use reflection

There are two programmers, one Alex, one is egon,alex in the writing process need to use the class of Egon, but Egon went to the honeymoon with his girlfriend, has not finished his class, Alex thought of the reflection, using the reflection mechanism Alex can continue to complete his code, After the return of Egon to the honeymoon and then continue to complete the definition of the class and to achieve the function Alex wanted.

In short, the advantage of reflection is that the interface can be defined in advance, the interface only after the completion of the actual execution, this implementation of Plug and Play, which is actually a ' late binding ', what does it mean? That is, you can write the main logic in advance (just define the interface), and then later to implement the function of the interface

Four, dynamic import module

1. Create a new t.py file

 1  print  (  " ----------------;   " )  2  def   Test1 ():  3  print  ("  test1   " )  4  5  def   _test2 ():  Span style= "COLOR: #008080" >6  print  ( test2   ") 

2, re-create: M1 folder, and then create a t.py below him

1module_t=__import__('m1.t')2 Print(module_t)3 Module_t.t.test1 ()4 #From m1.t Import *5 #From m1.t import Test,_test26 7 Importimportlib8M=importlib.import_module ('m1.t')9 Print(m)Ten M.test1 () OneM._test2 ()

Execution Result:

1 ---------------->2'M1' (namespace) >3test1 4 ' m1.t '  from ' d:\\python\\day26\\m1\\t.py '>5test16 test2

python-Object-oriented advanced

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.