You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Naively calling __str__() on an object assumes it is bound, which is not the case for non-instantiated classes.
Would it be possible to use Python's str builtin?
Can you explain when this is a problem when using PythonScript?
Let's take editor.write for example. Its C++ signature expects a boost::python::api::object.
As I understand, we can pass any Python object and you convert it to string via ScintillaWrapper::getStringFromObject by calling o.__str__().
This works because you usually pass an instantiated class object, meaning you don't need to manually pass self to its methods (like __str__), so a call without arguments works.
But it breaks if you pass an uninstantiated class object (class itself). It doesn't bind self, so it expects you to pass something to the __str__ method.
So far it looks fine with instances of str and int class (return values are omitted for brevity). Let's try a custom class.
>>>classFoo:
... def__str__(self):
... return__class__.__name__.lower()
...
>>>editor.write(Foo()) # Foo().__str__() == 'foo'>>>editor.write(Foo) # There is no `self` to auto insert into the `__str__` call.Traceback (mostrecentcalllast):
File"<console>", line1, in<module>TypeError: Foo.__str__() missing1requiredpositionalargument: 'self'
One might expect the value of str(Foo) to be written into the editor, but it wants us to pass something to __str__.
P.S. In this example str(Foo) == "<class '__main__.Foo'>". Not the most common use case, but you never know what the user is up to!
Activity
Ekopalypse commentedon Jul 11, 2024
Can you explain when this is a problem when using PythonScript?
tooruu commentedon Jul 11, 2024
Let's take
editor.writefor example. Its C++ signature expects aboost::python::api::object.As I understand, we can pass any Python object and you convert it to string via
ScintillaWrapper::getStringFromObjectby callingo.__str__().This works because you usually pass an instantiated class object, meaning you don't need to manually pass
selfto its methods (like__str__), so a call without arguments works.But it breaks if you pass an uninstantiated class object (class itself). It doesn't bind
self, so it expects you to pass something to the__str__method.So far it looks fine with instances of
strandintclass (return values are omitted for brevity). Let's try a custom class.One might expect the value of
str(Foo)to be written into the editor, but it wants us to pass something to__str__.P.S. In this example
str(Foo) == "<class '__main__.Foo'>". Not the most common use case, but you never know what the user is up to!Ekopalypse commentedon Jul 12, 2024
I understand what you mean, but usually PythonScript is used to manipulate Notepad++, Scintilla and the buffer content, but
I guess you're right
tooruu commentedon Jul 12, 2024
This isn't high priority, or might not even get addressed. Regardless, it is a bug so I had to bring it to your attention.