-
I have a class in uPython that I want to interact with in CPython.
This works. But when I try to implement a Has anyone tried something like this? What method(s) did you use to successfully proxy a class in C for a uPy class? |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 6 replies
-
As an experiment, I just wrote up a |
Beta Was this translation helpful? Give feedback.
-
Thank you for such outstanding engagement and support. It is uncommon. This is very interesting. However, I'm going to have to study how I do have a concern which might be unwarranted. At the CPython REPL:
|
Beta Was this translation helpful? Give feedback.
-
A relatively terse explanation:
This is a sharp corner of python involving class-vs-instance attributes, as well as mutable vs immutable objects. Here's a demo/explanation: class Foo:
int_a = 1
mutable = []
def __init__(self, int_b):
self.int_b = int_b
foo = Foo(2)
# First, lets demo `int_a`; these all point to the exact same object.
print(foo.int_a) # 1; `int_a` doesn't exist in the `foo` **instance**, so it looks it up in the `Foo` **class**
# The following 3 prints are different ways of saying the **exact same thing**
print(Foo.int_a) # 1
print(foo.__class__.int_a) # 1
print(type(foo).int_a) # 1
print()
# Now, lets set the instance `foo`'s int_a
foo.int_a = 100
print(foo.int_a) # 100
print(foo.__class__.int_a) # 1
# When we set `foo.int_a`, it wrote the value *just for that instance*.
# Now when we do the `foo.int_a` lookup, it's looking at it's own `int_a`, **not the classes**
print()
# But now lets see what happens when we modify a mutable object, such as a list:
foo.mutable.append("I added this string to the instance!")
print(foo.mutable) # ['I added this string to the instance!']
print(Foo.mutable) # ['I added this string to the instance!']
# when we did get `foo.mutable`, that attribute doesn't exist in the `foo` instance,
# but it does exist in the `Foo` **class**, so we got the list from `Foo.mutable`.
# Then, we did the `append` operation, which adds an element to that list (it's mutable).
# This means that, unless directly overwritten at the instance-level, all objects of Foo
# will share the same `mutable` list. |
Beta Was this translation helpful? Give feedback.
-
Many thanks! I have learned a lot from you. The |
Beta Was this translation helpful? Give feedback.
-
A couple observations from my testing:
I'll keep testing. |
Beta Was this translation helpful? Give feedback.
-
@kc64 can you try out the
Assuming all seems good, I'll just need to write up some docs before merging. |
Beta Was this translation helpful? Give feedback.
-
I’m sorry, but I am out of town and away from work for the week. I will be
eager to see your work when I return.
One thing I want to try is something I read in my research. According to
one post, if you define a new dictionary, repoint your class __dict__ to
it, then it will break the __setattr__ trigger. I’ll find out.
…On Tue, Feb 25, 2025 at 9:26 AM Brian Pugh ***@***.***> wrote:
@kc64 <https://github.com/kc64> can you try out the proxy-object branch
again? I fixed the 2 issues you brought up. The subclassing has a bit of a
quirk:
1. If you are setting non-proxy attributes (e.g. in your __init__),
you have to use object.__setattr__, otherwise it will (understandably)
set the attribute on-device.
2. Conventional getting/setting an attribute defined by (1) will just
work (gets/sets it locally, not on-device).
Assuming all seems good, I'll just need to write up some docs before
merging.
—
Reply to this email directly, view it on GitHub
<#175 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAGO2L4BYSEH2SURGY5K2OT2RR4TBAVCNFSM6AAAAABXR427BCVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTEMZRGM4TENA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
This is now in v0.23.0! In addition to everything we previously discussed, I:
|
Beta Was this translation helpful? Give feedback.
This is now in v0.23.0! In addition to everything we previously discussed, I:
Device.proxy("some_remote_object_name_here")
that creates aProxyObject
from a device.examples/11_proxy_objects
to demonstrate usage.