473,408 Members | 2,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

How do i Clone an object?

32
I am having trouble trying to clone a tree i made. Any suggestions?

My Input:
Expand|Select|Wrap|Line Numbers
  1. from cs1graphics import *
  2.  
  3. paper = Canvas(300, 200, 'skyBlue', 'My Forest')
  4.  
  5. grass = Rectangle(300, 80, Point(150,160))
  6. grass.setFillColor('green')
  7. grass.setBorderColor('green')
  8. grass.setDepth(75)                
  9. paper.add(grass)
  10.  
  11. tree1 = ('tree', 'tree_bottom', 'tree_base')
  12. tree = Polygon(Point(50,80), Point(30,125), Point(70,125))
  13. tree.setFillColor('darkGreen')
  14. paper.add(tree)
  15.  
  16. tree_bottom = Polygon(Point(50,80), Point(30,150), Point(70,150))
  17. tree_bottom.setFillColor('darkGreen')
  18. paper.add(tree_bottom)
  19.  
  20. tree_base = Polygon(Point(50,150), Point(45,165), Point(55,165))
  21. tree_base.setFillColor('brown')
  22. paper.add(tree_base)
  23.  
  24. tree2 = tree1.clone()
  25. tree2.move(170,30)
  26. tree2.scale(1.2)
  27. paper.add(tree2)
  28.  
Error:
Expand|Select|Wrap|Line Numbers
  1. Traceback (most recent call last):
  2.   File "C:/Users/Robert/Documents/CSIS Projects/Ex3_8.py", line 30, in <module>
  3.     tree2 = tree1.clone()
  4. AttributeError: 'tuple' object has no attribute 'clone'
  5.  
Sep 13 '10 #1

✓ answered by bvdet

I can see how you got the error. This will not work, because a tuple object has no method named clone:
Expand|Select|Wrap|Line Numbers
  1. tree1 = ('tree', 'tree_bottom', 'tree_base')
  2. tree2 = tree1.clone()
What is tree2 supposed to be?

2 4460
bvdet
2,851 Expert Mod 2GB
I can see how you got the error. This will not work, because a tuple object has no method named clone:
Expand|Select|Wrap|Line Numbers
  1. tree1 = ('tree', 'tree_bottom', 'tree_base')
  2. tree2 = tree1.clone()
What is tree2 supposed to be?
Sep 13 '10 #2
dwblas
626 Expert 512MB
"clone" is too vague. You have at least 2 options with a tuple/list
tree2 = tree1
creates a reference to tree1, i.e. they both point to the same block of memory, so if you change one, you change both. For a copy, you have to convert to a list.
Expand|Select|Wrap|Line Numbers
  1. import copy
  2. tree1 = ('tree', 'tree_bottom', 'tree_base')
  3. tree2 = tree1
  4. print tree2
  5. print id(tree2), id(tree1)   ## they are the same
  6.  
  7. tree3 = copy.copy(list(tree1))   ## shallow copy (look it up)
  8. print tree3
  9. print id(tree3), id(tree1)   ## they are different 
Sep 13 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Jason Evans | last post by:
Hi All, I am writing my own implementation of queue via a linked list, note not a LinkedList, and was running into trouble with the clone method. I was wondering if anyone could point out some...
4
by: Vincent | last post by:
Hey, I have a problem to understand the underlaying of clone method. Hope someone can help me out. Let's say, clonedObject = originalObject.clone() (syntax may be wrong, but you know what I...
2
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the...
13
by: ahaupt | last post by:
Hi all, I'm implementing the Clone() method through the ICloneable interface and don't quite know how deep I need to go for a deep copy. Example: class A: ICloneable { object _val;
6
by: solex | last post by:
Hello, I am trying to use serialization to copy objects. The object in question "Institution" inherits from a parent object "Party" both are marked as <Serializable()>. Initially I can copy an...
1
by: Alex D. | last post by:
hi guys. I need to clone multiple times an object and I am succesfully cloning using the regular serialization process, using a MemoryStream. My problem is that after cloning the object more that...
16
by: Hamed | last post by:
Hello I am developing a utility to be reused in other programs. It I have an object of type Control (a TextBox, ComboBox, etc.) that other programmers use it in applications. they may set some...
14
by: Hamed | last post by:
Hello It seems that I should implement ICloneable to implement my own clone object. the critical point for me is to make a control object based on another control object that all of its event...
15
by: mark.norgate | last post by:
Hello I want to create a reference to an object, so that changes to the referenced object are reflected in the other object. Like this: object o = 123; object p = o; o = 456;
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.