PyObjC is a bidirectional bridge between the
Python and
Objective-C
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its N ...
programming languages, allowing programmers to use and extend existing Objective-C
libraries
A library is a collection of materials, books or media that are accessible for use and not just for display purposes. A library provides physical (hard copies) or digital access (soft copies) materials, and may be a physical location or a vir ...
, such as
Apple
An apple is an edible fruit produced by an apple tree (''Malus domestica''). Apple trees are cultivated worldwide and are the most widely grown species in the genus '' Malus''. The tree originated in Central Asia, where its wild ances ...
's
Cocoa
Cocoa may refer to:
Chocolate
* Chocolate
* ''Theobroma cacao'', the cocoa tree
* Cocoa bean, seed of ''Theobroma cacao''
* Chocolate liquor, or cocoa liquor, pure, liquid chocolate extracted from the cocoa bean, including both cocoa butter and ...
framework, using Python.
PyObjC is used to develop
macOS
macOS (; previously OS X and originally Mac OS X) is a Unix operating system developed and marketed by Apple Inc. since 2001. It is the primary operating system for Apple's Mac (computer), Mac computers. Within the market of ...
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, or execution of a plan, idea, model, design, specification, standard, algorithm, or policy.
Industry-specific definitions
Computer science
In computer science, an implementation is a real ...
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 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 general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its NeXTS ...
, 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 operating system developed and marketed by Apple Inc. since 2001. It is the primary operating system for Apple's Mac computers. Within the market of desktop and lapt ...
. 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 Compute ...
, the predecessor to the current Apple platform
IDE,
Xcode.
Apple incorporated PyObjC into Mac OS X in 2007, with the release of
Mac OS X 10.5 Leopard
Mac OS X Leopard (version 10.5) is the sixth major release of macOS, Apple's desktop and server operating system for Macintosh computers. Leopard was released on October 26, 2007 as the successor of Mac OS X 10.4 Tiger, and is available in two ...
.
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
References
External links
*
Ronald Oussoren's warning on Xcode 4.0
{{DEFAULTSORT:Pyobjc
Python (programming language) libraries
MacOS programming tools