Python's Monkey Patch

Source: Internet
Author: User

1. Dynamic substitution of properties, methods, functions, etc. at run time 2. The purpose is often to enhance the ability to extend legacy code by replacing, modifying,
 #  test2.py   Class   person:  def   Get_score (self): ret  = {"  e Nglish   ": 80,"  history   ' : 100, "  Chinese   ": 150}  return  ret 
 #  test3.py   def   Get_score (self):  return  dict ( Name=self. __class__ . __name__ , english=100,chinese=40,history=120) 
 from Import  Person  from Import Get_score def Momkeypatch4person ():     = Get_scoremomkeypatch4person ()if__name__"__main__"  :    print(person (). Get_score ())
In the above example, suppose that the person class Get_score method is to take data from the database, but the test is inconvenient, using the monkey patch, replace the Get_socre method, return to the simulation data

  

Python's Monkey Patch

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.