SO? The issue is still present, and it's really unpleasant
Deepcopy does not work , copied Copy from here. Can not copy Instances - Error: un(deep)copyable object of type <class 'counterObject'>. Simple examples do NOT work.
Deepcopy does not work.
Using Thonny Micropython on PICO.
Copy.py is NOT on the UF2 file for PICO - as much as the docs seem to reflect.
Copied Copy.py from site here.
Can not copy Instances :
Error: un(deep)copyable object of type <class 'counterObject'>
Simple examples do NOT work.
If Standard documented Features / Cmds don't work here , then what good is the MicroPython ???
I have invested in using this for Programs for robots that I build.
So far the OOPS Objects Structuring here - is pretty much Trash - without the ability to copy Objects.
- JAVA has no problem with this, what's the Problem here ???
StackOverflow ex :
https://stackoverflow.com/questions/42143461/make-copy-of-object-instance-in-python
class counterObject:
def __init__(self):
self.Value = 0
def incrementValue(self):
self.Value += 1
def printValue(self):
print(self.Value)
A = counterObject()
A.incrementValue()
A.printValue() ##Prints 1
import copy
B = copy.deepcopy(A)
## => Error: un(deep)copyable object of type <class 'counterObject'>
print( id(A), id(B) )
Even the Google example does not work :
https://www.google.com/search?q=micropython+howto+create+object+then+copy+object
This returns :
=> Error: un(deep)copyable object of type <class 'MyClass'>
How to Create an ObjectYou define a class and then create an instance (object) of that class by calling the class name as a function.python
class MyClass:
def __init__(self, name, value):
[self.name](http://self.name/) = name
self.value = value
## Create an object (instance of the class)
original_object = MyClass("example", 100)
- Deep Copy (copy.deepcopy())A deep copy creates a completely independent clone of the original object, including recursively copying all nested objects and data structures. Changes to the copy will not affect the original.
import copy
## Create a deep copy
deep_copied_object = copy.deepcopy(original_object)
## => Error: un(deep)copyable object of type <class 'MyClass'>
deepcopy function references a non-existant variable
In copy.py: https://github.com/micropython/micropython-lib/blob/f20d89c6aad9443a696561ca2a01f7ef0c8fb302/copy/copy.py#L163 the deepcopy function references the variable dispatch_table, which isn't initialized.
I just reported this on pfalcon's repository which is the "official" source of this repository.
https://github.com/pfalcon/micropython-lib/issues/26
Simply speaking the OOPS side on this Micropython for PICO - is just Useless Trash.
-- Not functional - Right ???
I have to write my own Object Handler to work around this Failure ? - Are you kidding me ??
-- This is sad !
Think some time, you could actually Finish Code and Post code - that actually Works ???
The
copy.deepcopyfunction attempts to duplicate an object using several different ways, returning the error you see if no attempts were successful. Most of these depend on methods that are automatically added to objects at creation time by CPython itself (eg.__reduce__). MicroPython does not add these methods to object instances to minimise the footprint of the interpreter binary and of the in-memory representation of the instances themselves, since one may argue that object deep copies are sort of an anti-pattern for memory-constrained environments. The implementation of thecopymodule you find in this repo is almost identical as the one you may find in your CPython'ssys.path, to maintain compatibility with existing code that may leverage those extra extension points to customise the new object creation process.If
copy.deepcopyis indeed the only way to solve your issue, you can always provide your own__deepcopy__implementation for your class, as described in CPython'scopymodule documentation.I agree this limitation should have probably been added to either MicroPython's documentation or in a README.md file inside the
copymodule's directory in the first place (a PR is coming up soon after this), but the wording of the issue in question could have been so much better.