0

Ok, so if you have a class instance assigned to 'Bill' i understand you can't just use 'karl = bill' to make 'karl' an independent instance with the same parameters as bill. For the below class:

class person():
    def __init__(self,x=5,y=1,z=0):
        self.x = x
        self.y = y
        self.z= z



bill = person()

obviously karl = bill, just makes 'karl' a pointer to 'bill', so any changes made to 'karl' are actually changes made to bill. With only 3 variables, it'd be trivial to copy karl by the method of karl = person(bill.x,bill.y,bill.z) Yet, this becomes increasingly more tedious as you add class variables/parameters. haircolor, hairlenth, eye color, on and on. All the attributes that make up a person, for instance.

Is there no way to shorthand/automate the cloning of an instance of a class? Does python not keep an iterable list of a class' parameters so you can do something like "for n in bill._args:" or something?

or is this not the best way to develop classes that have a large contingent of variables?

Matt McCarthy
  • 89
  • 1
  • 5
  • 5
    Have you heard about [copy](https://docs.python.org/2/library/copy.html#copy.copy) and [deepcopy](https://docs.python.org/2/library/copy.html#copy.deepcopy) ? – Nir Alfasi Oct 09 '15 at 04:38
  • To see the contents of the object, try `bill.__dict__` Use `deepcopy` instead of `copy` if the object has a list or dictionary that you don't want shared. – Alexander Oct 09 '15 at 04:48
  • Watch out, though; it's quite possible that neither `copy.copy` nor `copy.deepcopy` will be the right way to copy an object, if some things should be shared and some shouldn't be, or if a copy needs to only be deep to a specific depth. For example, if you wanted to copy a tree iterator that tracks its position with a stack of nodes, you'd want to copy the stack, but not the nodes. Neither `copy.copy(iterator)` nor `copy.deepcopy(iterator)` would do that. This can be somewhat handled by customizing how `copy` and `deepcopy` handle your objects. – user2357112 Oct 09 '15 at 04:51
  • @Alexander dude... first off thank you.. secondly that is exactly what i was looking for, actually all the given answers so far are immensly helpful. I guess my addendum is ... *infuriated* WHY DOESN'T __dict__ show up in the interpreter shell as a possible option? even when i ._ it's not there. – Matt McCarthy Oct 09 '15 at 04:53
  • @MattMcCarthy: Are you on Python 2? Make `person` inherit from `object`; on Python 2, every class should subclass `object` if it doesn't subclass anything else, or you'll get old-style classes, which are worse in a variety of ways. – user2357112 Oct 09 '15 at 04:57

2 Answers2

1

use copy:

In [1]: class person():
   ...:         def __init__(self,x=5,y=1,z=0):
   ...:                 self.x = x
   ...:                 self.y = y
   ...:                 self.z= z
   ...:

In [2]: p = person()

In [3]: import copy
In [5]: copy_of_p = copy.copy(p)

In [6]: id(copy_of_p)
Out[6]: 4524984368

In [7]: id(p)
Out[7]: 4525007432

In [8]: copy_of_p.x = 50

In [9]: p.x
Out[9]: 5
salparadise
  • 5,699
  • 1
  • 26
  • 32
1

Referring to the class person in your code above:

import copy 
bill = person()
billsShallowClone = copy.copy(bill)
shafeen
  • 2,431
  • 18
  • 23