Im currently porting a Python Script to work in Swift using the PythonKit library, and I'm trying to print out to the console what properties a given PythonObject has.
So I take the following steps to try and work out what dynamic properties a PythonObject has.
let object: PythonObject = SomeObject
let inspect = Python.import("inspect")
// Print out the class name of this object
print(object.__class__.__name__)
// Get the 'callable' function from python
let callableFunc = Python.type("callable")
// List all the properties of the object using the python function __dir__() this equates to just using dir()
for fun in object.__dir__()
{
// Get the member name
if let memberName = String(fun)
{
// Call the callable function, and convet its return type to a Bool
if let canCall = Bool(callableFunc(fun))
{
// If true then it is callable
if canCall
{
// NEVER GETS HERE :(
// Use the inspect import to check the signature of the function
if let params = String(inspect.signature(fun))
{
// Print some details
print(String(memberName) + params)
}
}
else
{
// ALWAYS GETS HERE
// If it fails just print the member name
print(memberName)
}
}
else
{
// If it fails just print the member name
print(memberName)
}
}
}
As you can see in my code example the callable function always fails and says every property is not callable.
Any ideas why this could be? I've read in other posts about something similar in Python if someone calls the 'callable' function using a python function with the parentheses brackets the callable function returns false IE
class Pen:
def write(self):
print("Writing")
def __call__(self,p):
print("calling a pen object")
pen1 = Pen()
print(callable(pen1.write())) prints false
print(callable(pen1.write)) prints true
Am I missing a trick with the PythonObject in Swift land?