Prototype (Python)

Source: Internet
Author: User

Prototype: Use a prototype instance to specify the type of the object to be created, and copy the prototype to create a new object.
The prototype is essentiallyClone object, So inObject initialization operations are complex, which can greatly reduce time consumption and improve performance.Because "you do not need to re-initialize the object, but dynamically obtain the object runtime status ".

Shortest copy (Shallow copy): The object field is copied, but the object referenced by the field is not copied. The Copied object and the source object only share the same name, but share the same object.
Deep copy (Deep copy): The object referenced by fields in the object instance is also copied.

# Encoding = UTF-8 ## by Panda # prototype mode import copydef printinfo (Info): Print Unicode (Info, 'utf-8 '). encode ('gbk') # copy Interface Class icloneable: def shallowclone (Self): Return copy. copy (Self) def deepclone (Self): Return copy. deepcopy (Self) # Work Experience class workexperience (icloneable): workdata = "" Company = "" pass # resume class resume (icloneable ): name = "" Sex = 'unknown 'age = 0 work = none def _ init _ (self, name, work = workexperience (): Self. name = Name self. work = work; def setpersoninfo (self, Sex, Age): Self. sex = sex self. age = age def setworkexperience (self, workdata, company): Self. work. workdata = workdata self. work. company = company def display (Self): printinfo ('% s, % s, % d' % (self. name, self. sex, self. age) printinfo ('% s, % s' % (self. work. workdata, self. work. company) def clientui (): A = resume ('daybird '). setpersoninfo ('male', 29). setworkexperience ("1998-2000", "XX Company") # copy B =. shallowclone () B. setworkexperience ("2000-2006", "YY Company") # Deep copy c =. deepclone () C. setworkexperience ("2006-2009", "ZZ company") B. display (). display () C. display () returnif _ name _ = '_ main _': clientui ();

Class diagram:

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.