@dynamicCallable
@dynamicMemberLookup
public struct PythonObject

PythonObject represents an object in Python and supports dynamic member lookup. Any member access like object.foo will dynamically request the Python runtime for a member with the specified name in this object.

PythonObject is passed to and returned from all Python function calls and member references. It supports standard Python arithmetic and comparison operators.

Internally, PythonObject is implemented as a reference-counted pointer to a Python C API PyObject.

  • Creates a new instance, taking ownership of the specified PyObject pointer.

    Declaration

    public init(owning pointer: OwnedPyObjectPointer)
  • Creates a new instance from the specified PyObject pointer.

    Declaration

    public init(borrowing pointer: UnsafeMutablePointer<PyObject>)
  • A textual description of this PythonObject, produced by Python.str.

    Declaration

    public var description: String { get }
  • Declaration

    public var customMirror: Mirror { get }
  • Declaration

    public init(_ object: PythonObject)
  • Declaration

    public var pythonObject: PythonObject { get }
  • Returns a callable version of this PythonObject. When called, the result throws a Swift error if the underlying Python function throws a Python exception.

    Declaration

    var throwing: ThrowingPythonObject { get }
  • Declaration

    subscript(dynamicMember memberName: String) -> PythonObject { get nonmutating set }
  • Access the element corresponding to the specified PythonConvertible values representing a key.

    Note

    This is equivalent to object[key] in Python.

    Declaration

    subscript(key: PythonConvertible...) -> PythonObject { get nonmutating set }
  • Converts to a 2-tuple.

    Declaration

    var tuple2: (PythonObject, PythonObject) { get }
  • Converts to a 3-tuple.

    Declaration

    var tuple3: (PythonObject, PythonObject, PythonObject) { get }
  • Converts to a 4-tuple.

    Declaration

    var tuple4: (PythonObject, PythonObject, PythonObject, PythonObject) { get }
  • Call self with the specified positional arguments.

    Precondition

    self must be a Python callable.

    Declaration

    @discardableResult
    func dynamicallyCall(
      withArguments args: [PythonConvertible] = []
    ) -> PythonObject

    Parameters

    args

    Positional arguments for the Python callable.

  • Call self with the specified arguments.

    Precondition

    self must be a Python callable.

    Declaration

    @discardableResult
    func dynamicallyCall(
      withKeywordArguments args:
        DictionaryLiteral<String, PythonConvertible> = [:]
    ) -> PythonObject

    Parameters

    args

    Positional or keyword arguments for the Python callable.

  • Declaration

    static func + (lhs: PythonObject, rhs: PythonObject) -> PythonObject
  • Declaration

    static func - (lhs: PythonObject, rhs: PythonObject) -> PythonObject
  • Declaration

    static func * (lhs: PythonObject, rhs: PythonObject) -> PythonObject
  • Declaration

    static func / (lhs: PythonObject, rhs: PythonObject) -> PythonObject
  • Declaration

    static func += (lhs: inout PythonObject, rhs: PythonObject)
  • Declaration

    static func -= (lhs: inout PythonObject, rhs: PythonObject)
  • Declaration

    static func *= (lhs: inout PythonObject, rhs: PythonObject)
  • Declaration

    static func /= (lhs: inout PythonObject, rhs: PythonObject)
  • Declaration

    public init<T>(exactly value: T) where T : BinaryInteger
  • Declaration

    public typealias Magnitude = PythonObject
  • Declaration

    public var magnitude: PythonObject { get }
  • Declaration

    public static func == (lhs: PythonObject, rhs: PythonObject) -> Bool
  • Declaration

    public static func != (lhs: PythonObject, rhs: PythonObject) -> Bool
  • Declaration

    public static func < (lhs: PythonObject, rhs: PythonObject) -> Bool
  • Declaration

    public static func <= (lhs: PythonObject, rhs: PythonObject) -> Bool
  • Declaration

    public static func > (lhs: PythonObject, rhs: PythonObject) -> Bool
  • Declaration

    public static func >= (lhs: PythonObject, rhs: PythonObject) -> Bool
  • Declaration

    public var hashValue: Int { get }
  • Declaration

    public typealias Index = PythonObject
  • Declaration

    public typealias Element = PythonObject
  • Declaration

    public var startIndex: Index { get }
  • Declaration

    public var endIndex: Index { get }
  • Declaration

    public func index(after i: Index) -> Index
  • Declaration

    public subscript(index: PythonObject) -> PythonObject { get set }
  • Declaration

    public init(arrayLiteral elements: PythonObject...)
  • Key

    Declaration

    public typealias Key = PythonObject
  • Declaration

    public typealias Value = PythonObject
  • Declaration

    public init(dictionaryLiteral elements: (PythonObject, PythonObject)...)