Python - DeepCopy of custom class instance

Hi,
I’m trying to make a deepCopy of a custom python class instance, but it is miserably failing.
I suspect that I’m missing some basic theory here. Any help on understanding why is happening this?
thanks!

hi @aitorleceta

Where is the instance being created? what do you get when you print shoe before you try to make a copy?

If you can post your script (or a prototype that describes the error), it’d be much easier to help you…

1 Like

hi @DavidLeon class definition is on a python component in the GH document. An instance of the class is saved to sticky in other python component. Finally, on other python component, I’m loading the instance from sticky and trying to execute a deepcopy. Please, tell me if the explanation is not enough.

I’ve tried the same steps with other simple dummy class and it is working without problems. So, I deduce that error is causing the particular content of my class. It’s quite complex class so it has no sense to share it, but maybe it is significant to say that it contains Rhinocommon data types as attributes, including geometry, DocObjets (curve objects, subd objects, layers,…) Maybe its the rhinocommon data types wich are causing problems. In fact, the error message is referring to “Rhino.RhinoDoc does not define any public constructor”.
Oh, in fact, my class has some references to Rhino.RhinoDoc.ActiveDoc…!? f
like here…

Should I avoid that my classes are internally referencing to RhinoDoc they are “living on”? certainly, It doesn’t seem to be the best design …
Anyway, I would be nice to hear your opinions prior to starting to change my code all around… thanks!

Edit: In the case that what I commented before is the problematic part, then i’m in a trouble: I could easily pass doc related values as “ModelAbsoluteTolerace” to the constructor, but my classes are using some Rhinocommon method related to the particular document they “live”, as find methods in doc tables…!!!

Hi,

just create your own static copy method, and define yourself what is copied and what is not. In my understanding a deep copy copies all instances within a class, but this something you obviously don’t want.

1 Like

Thank tom.
Definitively the error was on having as attribute in my classes the Rhino.RhinoDoc.ActiveDoc object. An horrible decision from my part.
I get ride of them, and now deepcopy is working fine
thanks!.

Why is this? Holding a reference to another object is no bad design at all, and often required. Doing a deep copy rather is.

Whats not good is to expose such a static reference (ActiveDoc) not within a constructor, so that when someone else is referencing your class outside of Rhino it throws, and nobody knows why. Services, Singeltons or static member should always be part of the constructor argument or being created through a factory pattern. Inversion of control basics…

1 Like

Thanks tom I will definitively follow you advice. For the time bien I will inject ActiveDoc when constructing.
I’m using deepcopy as quick way to have a new version of the instance the user is working on. In my mental schema there is no problem with deepcopy, as I dont “see” any shared data between instances.

Anyway, I need to study “inversion of control” for a future refactoring step, thanks for the guidance!

by the way, the error, in particular, was that I was using the hole ActiveDoc as instance attribute (attention, Coding Horror!!)

    class MyClass():
         def __init__(self,args):
              self.doc = Rhino.RhinoDoc.ActiveDoc # don't do this!!!
2 Likes