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
+ 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 from cs1graphics import *   paper = Canvas(300, 200, 'skyBlue', 'My Forest')   grass = Rectangle(300, 80, Point(150,160)) grass.setFillColor('green') grass.setBorderColor('green') grass.setDepth(75)                 paper.add(grass)   tree1 = ('tree', 'tree_bottom', 'tree_base') tree = Polygon(Point(50,80), Point(30,125), Point(70,125)) tree.setFillColor('darkGreen') paper.add(tree)   tree_bottom = Polygon(Point(50,80), Point(30,150), Point(70,150)) tree_bottom.setFillColor('darkGreen') paper.add(tree_bottom)   tree_base = Polygon(Point(50,150), Point(45,165), Point(55,165)) tree_base.setFillColor('brown') paper.add(tree_base)   tree2 = tree1.clone() tree2.move(170,30) tree2.scale(1.2) paper.add(tree2)   Error: Expand|Select|Wrap|Line Numbers Traceback (most recent call last):   File "C:/Users/Robert/Documents/CSIS Projects/Ex3_8.py", line 30, in      tree2 = tree1.clone() AttributeError: 'tuple' object has no attribute 'clone'   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
2 Replies

 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 tree1 = ('tree', 'tree_bottom', 'tree_base') 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 import copy tree1 = ('tree', 'tree_bottom', 'tree_base') tree2 = tree1 print tree2 print id(tree2), id(tree1)   ## they are the same   tree3 = copy.copy(list(tree1))   ## shallow copy (look it up) print tree3 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.