or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

categories.mdcore-bridge.mddecorators.mdframework-loading.mdindex.mdprotocols.mdtype-system.mdutilities.md
tile.json

tessl/pypi-pyobjc

PyObjC is a bridge between Python and Objective-C that allows full featured Cocoa applications to be written in pure Python.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/pyobjc@10.3.x

To install, run

npx @tessl/cli install tessl/pypi-pyobjc@10.3.0

index.mddocs/

PyObjC

PyObjC is a comprehensive Python-to-Objective-C bridge that enables seamless integration between Python and Objective-C code on macOS. It allows developers to create full-featured Cocoa applications in pure Python, access macOS system frameworks, and mix Python with Objective-C, C, and C++ code.

Package Information

  • Package Name: pyobjc
  • Language: Python
  • Installation: pip install pyobjc
  • Documentation: https://pyobjc.readthedocs.io/en/latest/

Core Imports

import objc

For utility tools:

from PyObjCTools import KeyValueCoding, TestSupport

Basic Usage

import objc
from Foundation import NSString, NSArray

# Working with Objective-C classes
hello = NSString.stringWithString_("Hello, World!")
print(hello.length())  # 13

# Creating and using Objective-C objects
array = NSArray.arrayWithObjects_("foo", "bar", "baz", None)
print(array.count())  # 3
print(array.objectAtIndex_(1))  # bar

# Using the bridge constants
print(objc.YES)  # True
print(objc.NO)   # False
print(objc.nil)  # None

# Proper memory management with autorelease pools
with objc.autorelease_pool():
    # Intensive Objective-C operations that create temporary objects
    for i in range(1000):
        temp_string = NSString.stringWithFormat_("Item %d", i)
        # Objects are automatically released when pool exits

# Manual pool management (less common)
objc.recycleAutoreleasePool()

Architecture

PyObjC consists of two main components:

  • pyobjc-core: The fundamental bridge implementation that handles Python-Objective-C interoperability, method calling, memory management, and type conversion
  • Framework packages: 100+ specialized packages (pyobjc-framework-*) that provide Python bindings for specific macOS frameworks

Framework Package Structure

PyObjC provides comprehensive coverage of macOS frameworks through specialized packages:

Core Frameworks:

  • pyobjc-framework-Cocoa - Foundation, AppKit, CoreFoundation (essential UI and system APIs)
  • pyobjc-framework-CoreData - Object-relational mapping and data persistence
  • pyobjc-framework-CoreGraphics - 2D graphics rendering and PDF generation

Media and Graphics:

  • pyobjc-framework-AVFoundation - Audio/video capture, playback, and editing
  • pyobjc-framework-Metal - GPU programming and graphics acceleration
  • pyobjc-framework-Vision - Computer vision and image analysis
  • pyobjc-framework-CoreImage - Image processing and filtering

Web and Networking:

  • pyobjc-framework-WebKit - Web browser engine integration
  • pyobjc-framework-Network - Modern networking APIs
  • pyobjc-framework-CFNetwork - Core networking functionality

Development Tools:

  • pyobjc-framework-Quartz - PDF and graphics utilities
  • pyobjc-framework-ScriptingBridge - Application automation

Each framework package uses lazy loading for optimal performance and automatically handles framework initialization, type bridging, and method signature resolution. The bridge provides seamless integration with over 100 system frameworks while maintaining Pythonic interfaces and automatic memory management.

Capabilities

Core Bridge Functions

Fundamental bridge functionality including class lookup, runtime access, memory management, and bridge configuration. These functions provide the foundation for all Python-Objective-C interoperability.

def lookUpClass(name: str): ...
def getClassList(): ...
def recycleAutoreleasePool(): ...
options: object  # Bridge configuration (objc.options.verbose = True/False)

Core Bridge Functions

Method and Class Decorators

Decorators and utilities for creating Objective-C methods, defining method signatures, creating categories, and handling protocol conformance. Essential for creating Python classes that integrate with Objective-C.

def signature(signature: str): ...
def callbackFor(signature: str): ...
def category(baseClass): ...
def synthesize(name: str, copy=None): ...

Method and Class Decorators

Framework and Library Loading

Dynamic loading of macOS frameworks and libraries, bundle resource access, and framework initialization. Enables runtime access to system frameworks and custom libraries.

def dyld_framework(path: str): ...
def pathForFramework(name: str): ...
def inBundle(bundlePath: str): ...

Framework Loading

Type System and Bridging

Type encoding, signature parsing, struct type creation, and bridge support utilities. Handles the complex type conversion between Python and Objective-C type systems.

def splitSignature(signature: str): ...
def createStructType(name: str, signature: str, typeids): ...
def registerMetaDataForSelector(classname: str, selector: str, metadata): ...

Type System

Protocol Support

Protocol management, formal and informal protocol definition, and protocol conformance checking. Essential for implementing Objective-C protocols in Python.

def protocolNamed(name: str): ...
def informal_protocol(name: str, selectors): ...
def formal_protocol(name: str, supers, selectors): ...

Protocol Support

Utility Tools

Key-Value Coding, testing support, signal handling, and other utility functions. Provides additional functionality for common PyObjC development patterns.

def getKey(object, key: str): ...
def setKey(object, key: str, value): ...
class TestCase: ...

Utility Tools

Categories and Method Addition

Dynamic method addition to existing Objective-C classes through categories. Enables extending system frameworks and third-party classes without subclassing.

def category(baseClass): ...
def classAddMethod(targetClass, method, targetMethod): ...
class Category: ...

Categories and Method Addition

Constants and Types

# Bridge Constants
nil = None
YES = True  
NO = False

# Core Classes
class NSObject: 
    """Base class for all Objective-C objects"""
    
class selector: 
    """Represents an Objective-C selector (method name)"""
    
class objc_method: 
    """Represents an Objective-C method with type information"""
    
class autorelease_pool:
    """Context manager for autorelease pool management"""
    def __enter__(self): ...
    def __exit__(self, exc_type, value, tp): ...

# Exception Classes
class error(Exception): 
    """Base PyObjC exception"""
    
class nosuchclass_error(Exception): 
    """Raised when an Objective-C class cannot be found"""
    
class internal_error(Exception): 
    """Raised for internal PyObjC errors"""

# Type Encoding Constants
_C_ID: str       # '@' - Object pointer
_C_CLASS: str    # '#' - Class
_C_SEL: str      # ':' - Selector  
_C_CHR: str      # 'c' - char
_C_INT: str      # 'i' - int
_C_FLT: str      # 'f' - float
_C_DBL: str      # 'd' - double
_C_BOOL: str     # 'B' - BOOL
_C_VOID: str     # 'v' - void
_C_PTR: str      # '^' - pointer