Sorry if this has been asked before, but I can't really find anything about this.
Basically, the gist of it is:
class ExampleClass:
"""A simple example class"""
description = "Lorem"
desc = "Ipsum"
Importing this file causes a problem if I try to access a specific dynamically callable property that collides with a Swift property, i.e. it has the same name in both Python and Swift: The Swift property simply takes precedence.
let example = Python.import("example")
print(example.ExampleClass().description) // "<example.ExampleClass object at 0x10e2cbdd8>" <-- CustomStringConvertible's .description takes precedence
print(example.ExampleClass().desc) // "Ipsum"
Is there any way around this? It doesn't seem like there is a .get function or similar.
Dug a little more in the code, this should work:
// instead of
example.ExampleClass().description
// you can write
example.ExampleClass()[dynamicMember: "description"]
At least this is Googleable now.
Yes, pythonObject[dynamicMember: "name"] is exactly right.
For reference, there are times when you may also need to call dynamicallyCall directly, namely when unpacking argument lists. There's no good solution for that yet though, since the * "splat" operator isn't officially exposed in the Python C API (AFAICT).
I'm curious how you were able to run Python.import("example"). Did you create and install a mini pip package?
@dan-zheng Thanks for the info! For demo purposes I just did this:
let sys = Python.import("sys")
sys.path.append(FileManager().currentDirectoryPath)
Then there's just an example.py in the working directory.
Most helpful comment
Dug a little more in the code, this should work:
At least this is Googleable now.