By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

How do i Clone an object?

P: 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?

Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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

Expert 100+
P: 621
"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

Post your reply

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