PyObjC is a bidirectional bridge between the
Python and
Objective-C
Objective-C is a high-level general-purpose, object-oriented programming language that adds Smalltalk-style message passing (messaging) to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was ...
programming languages, allowing programmers to use and extend existing Objective-C
libraries
A library is a collection of Book, books, and possibly other Document, materials and Media (communication), media, that is accessible for use by its members and members of allied institutions. Libraries provide physical (hard copies) or electron ...
, such as
Apple
An apple is a round, edible fruit produced by an apple tree (''Malus'' spp.). Fruit trees of the orchard or domestic apple (''Malus domestica''), the most widely grown in the genus, are agriculture, cultivated worldwide. The tree originated ...
's
Cocoa framework, using Python.
PyObjC is used to develop
macOS
macOS, previously OS X and originally Mac OS X, is a Unix, Unix-based operating system developed and marketed by Apple Inc., Apple since 2001. It is the current operating system for Apple's Mac (computer), Mac computers. With ...
applications in pure Python.
There is also limited support for
GNUstep
GNUstep is a free software implementation of the Cocoa (formerly OpenStep) Objective-C frameworks, widget toolkit, and application development tools for Unix-like operating systems and Microsoft Windows. It is part of the GNU Project.
GNUst ...
, an open source, cross-platform
implementation
Implementation is the realization of an application, execution of a plan, idea, scientific modelling, model, design, specification, Standardization, standard, algorithm, policy, or the Management, administration or management of a process or Goal ...
of Cocoa.
For Python programmers
The most important usage of PyObjC is enabling programmers to create
GUI applications using Cocoa libraries in pure Python. Moreover, as an effect of Objective-C's close relationship with the
C programming language (it is a pure superset), developers are also able to incorporate any C-based
API
An application programming interface (API) is a connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how to build ...
by
wrapping it with an Objective-C wrapper and then using the wrapped code over the PyObjC bridge. Using
Objective-C++
Objective-C is a high-level general-purpose, object-oriented programming language that adds Smalltalk-style message passing (messaging) to the C (programming language), C programming language. Originally developed by Brad Cox and Tom Love in ...
, the same can be done with
C%2B%2B libraries.
For Objective-C programmers
Cocoa developers may also benefit, as tasks written in Python generally take fewer lines than the Objective-C equivalent. This can be used to their advantage as it enables faster prototyping.
History
PyObjC's origins date back to 1996, when Lele Gaifax built the original module in September of that year. Among the credited contributors were Guido van Rossum, creator of the Python programming language.
PyObjC was rewritten in 2002. Notable additions include the ability to directly subclass Objective-C classes from Python and nearly complete support for the Foundation, App Kit and Address Book frameworks.
Later the same year, support was added for non-framework Python builds, as well as subsequent support for the Python distribution included with
Mac OS X
macOS, previously OS X and originally Mac OS X, is a Unix, Unix-based operating system developed and marketed by Apple Inc., Apple since 2001. It is the current operating system for Apple's Mac (computer), Mac computers. With ...
. Along with these changes came project templates for standalone Cocoa applications for use with
Project Builder
Project Builder was an integrated development environment (IDE) originally developed by NeXT for version 3 of the NeXTSTEP operating system by separating out the code editing parts of Interface Builder into its own application.
After Apple Compu ...
, the predecessor to the current Apple platform
IDE,
Xcode
Xcode is a suite of developer tools for building apps on Apple devices. It includes an integrated development environment (IDE) of the same name for macOS, used to develop software for macOS, iOS, iPadOS, watchOS, tvOS, and visionOS. It w ...
.
Apple incorporated PyObjC into Mac OS X in 2007, with the release of
Mac OS X 10.5 Leopard.
Messages and methods
In Objective-C, objects communicate with each other by sending messages, which is analogous to method calls in other object-oriented languages. When an object receives a message, it looks up the message's name, or selector, and matches it up with a method designated the same selector, which it then invokes.
The syntax for these message expressions is inherited from Smalltalk, and appears as an object, called the receiver, placed to the left of the name of the message, or selector, and both are enclosed within a pair of square brackets (the square bracket syntax is not inherited from Smalltalk). Colons within a selector indicate that it accepts one or more arguments, one for each colon. Intended to improve code readability, colons are placed within the selector such that when the required arguments are in place, the expression's intent is unambiguous:
yLittleDuck makeSomeNoise:quack eyesClosed:@YES onOneFoot:@YES
This is distinct from the syntax used in Python, and in many other languages, where an equivalent expression would read:
myLittleDuck.makeSomeNoise_eyesClosed_onOneFoot_(quack, True, True)
Translating Objective-C selectors to Python method names is accomplished by replacing each colon with a single underscore and listing the arguments within a pair of parentheses at the end, as demonstrated above.
Classes
Objective-C classes are subclassed in the same manner as a normal Python class:
class MyDuck(NSObject): # NSObject is a base Objective-C class.
def init(self):
self = super(MyDuck, self).init() # An Objective-C idiom, wherein the
# subclass instance, self, is instantiated
# by sending the superclass its
# designated initializer.
return self
myLittleDuckOne = MyDuck.alloc().init()
See also
*
libffi
*
RubyCocoa
RubyCocoa is a macOS framework that provides a bridge between the Ruby and the Objective-C programming languages, allowing the user to manipulate Objective-C objects from Ruby, and vice versa. It makes it possible to write a Cocoa application co ...
References
External links
*
Ronald Oussoren's warning on Xcode 4.0
{{DEFAULTSORT:Pyobjc
Python (programming language) libraries
MacOS programming tools