Package pycocoa :: Module runtime :: Class ObjCSubclass
[frames] | no frames]

Class ObjCSubclass

object --+    
         |    
  ObjCBase --+
             |
            ObjCSubclass

Python class creating an ObjC sub-class of an existing ObjC (super-)class.

This class is used only to *define* the interface and implementation of an ObjC sub-class from Python. It should not be used in any other way. If you want a Python representation of the resulting class, create it with ObjCClass.

It consists primarily of function decorators which you use to add methods to the sub-class.

ObjCSubclass is used to define an ObjC sub-class of an existing class registered with the runtime. When you create an instance of ObjCSubclass, it registers the new sub-class with the ObjC runtime and creates a set of function decorators that you can use to add instance methods or class methods to the sub-class.

Typical usage would be to first create and register the sub-class:

>>> MySubclass = ObjCSubclass('NSObject', 'MySubclassName')

then add methods with:

>>> @MySubclass.method('v')
>>> def methodThatReturnsVoid(self):
>>>     pass
>>> @MySubclass.method('Bi')
>>> def boolReturningMethodWithInt_(self, x):
>>>     return True
>>> @MySubclass.classmethod('@')
>>> def classMethodThatReturnsId(self):
>>>     return self

It is probably a good idea to organize the code related to a single sub-class by either putting it in its own module (note that you don't actually need to expose any of the method names or the ObjCSubclass) or by bundling it all up inside a Python class definition, perhaps called MySubclassImplementation.

It is also possible to add ObjC ivars to the sub-class, however if you do so, you must call the __init__ method with register=False, and then call the register method after the ivars have been added.

However, instead of creating the ivars in ObjC land, it is easier to just define Python-based instance variables in your sub-class's init method.

Instances are created as a pointer to the objc object by using:

>>> myinstance = send_message('MySubclassName', 'alloc')
>>> myinstance = send_message(myinstance, 'init')

or wrapped inside an ObjCInstance object by using:

>>> myclass = ObjCClass('MySubclassName')
>>> myinstance = myclass.alloc().init()
Instance Methods
 
__init__(self, parent, name, register=True, **ivars)
New sub-class of the given (super-)class.
 
__str__(self)
str(x)
 
add_ivar(self, name, ctype)
Add an instance variable to the sub-class.
 
classmethod(self, encoding)
Decorator for class methods.
 
method(self, encoding)
Decorator for instance methods.
 
rawmethod(self, encoding)
Decorator for instance methods without any fancy shenanigans.
 
register(self)
Register this new class with the ObjC runtime.

Inherited from ObjCBase: __repr__

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __subclasshook__

Properties
  name
Get the name of this ObjC sub-class (str).
  obj_class
Get the ObjC class.
  obj_metaclass
Get the ObjC metaclass, or None if un-registered.

Inherited from object: __class__

Method Details

__init__(self, parent, name, register=True, **ivars)
(Constructor)

 

New sub-class of the given (super-)class.

Parameters:
  • parent - The super-class (str or Object).
  • name - The sub-class name (str).
  • register - Optionally, register the new sub-class (bool).
  • ivars - Optionally, specify any number of instance variables to be added before registering the new class, each with a keyword argument ivarname=ctype to specify the name and ctype of the instance variable.
Overrides: object.__init__

__str__(self)
(Informal representation operator)

 

str(x)

Overrides: object.__str__
(inherited documentation)

add_ivar(self, name, ctype)

 

Add an instance variable to the sub-class.

Parameters:
  • name - Name of the ivar (str).
  • ctype - The ivar type (ctypes).
Raises:
  • ValueError - Class is already registered.

Note: Instance variables can only be added BEFORE the class is registered.

classmethod(self, encoding)

 

Decorator for class methods.

Parameters:
  • encoding - Signature of the method (encoding).
Returns:
Decorater.

method(self, encoding)

 

Decorator for instance methods.

Parameters:
  • encoding - Signature of the method (encoding).
Returns:
Decorater.

rawmethod(self, encoding)

 

Decorator for instance methods without any fancy shenanigans.

Parameters:
  • encoding - Signature of the method (encoding).
Returns:
Decorater.

Note: The method must have signature m(self, cmd, *args) where both self and cmd are just pointers to ObjC objects.


Property Details

name

Get the name of this ObjC sub-class (str).

Get Method:
name(self) - Get the name of this ObjC sub-class (str).

obj_class

Get the ObjC class.

Get Method:
obj_class(self) - Get the ObjC class.

obj_metaclass

Get the ObjC metaclass, or None if un-registered.

Get Method:
obj_metaclass(self) - Get the ObjC metaclass, or None if un-registered.