0% found this document useful (0 votes)
111 views2 pages

Problem Wk.1.4.6: Oops: Part 1: Thing Clone

This document provides instructions for modifying the Thing class in two parts. Part 1 asks to add a clone method that returns a new Thing instance with the same stored value. Part 2 asks to add a __str__ method so that printing a Thing returns an informative string, such as "This is a Thing with value 3". Both parts instruct to use the class's set and get methods rather than directly accessing the private attribute.

Uploaded by

ssaurabh_ss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views2 pages

Problem Wk.1.4.6: Oops: Part 1: Thing Clone

This document provides instructions for modifying the Thing class in two parts. Part 1 asks to add a clone method that returns a new Thing instance with the same stored value. Part 2 asks to add a __str__ method so that printing a Thing returns an informative string, such as "This is a Thing with value 3". Both parts instruct to use the class's set and get methods rather than directly accessing the private attribute.

Uploaded by

ssaurabh_ss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Problem Wk.1.4.

6: OOPs
Part 1: Thing clone
Add a method called clone to the Thing class which returns a completely new Thing with
the same stored value.
>>>
>>>
>>>
>>>
3

a = Thing()

a.set(3)

b = a.clone()

b.get()

Use the set and get methods of Thing, do not access x directly.

Part 2: Thing str


Add a __str__ method to the Thing class so that printing a Thing instance generates an
informative string.
>>> a = Thing()

>>> a.set(3)

>>> print a

This is a Thing with value 3

Use the set and get methods of Thing, do not access x directly.
Python has a built-in function str that will be useful. Look up its definition in the
documentation.

MIT OpenCourseWare
http://ocw.mit.edu

6.01SC Introduction to Electrical Engineering and Computer Science


Spring 2011

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy