[Pyobjc-dev] Re: [Pythonmac-SIG] pyobjc / cocoa

Bill Bumgarner bbum@codefab.com
Thu, 17 Oct 2002 14:29:23 -0400


On Thursday, October 17, 2002, at 02:01 PM, Bob Ippolito wrote:
> IMHO there should also be easy to use wrappers for NSNumber and 
> NSData.. I use those on a pretty regular basis for stuff that needs to 
> get serialized for DO or plists.  Another extremely convenient idea 
> would be to just make special cases for some of the most used 
> Foundation classes such as NS[Mutable]Array, NS[Mutable]Dictionary and 
> NSEnumerator so that you can treat them like native python objects.  
> Possibly even NS[Mutable]String and so on.  Probably wouldn't even 
> take too long to develop, especially if it's done (at least at first) 
> from the python end of the stick and not in ObjC.

Wrapping NSNumber and NSData is definitely on the radar, but there is 
some subtlety in how they need to be wrapped.   I totally agree that 
they should be wrapped in some fashion.

On the ObjC->Python front, do you mean:

[bumbox:~] bbum% python
Python 2.2 (#1, 07/14/02, 23:25:09)
[GCC Apple cpp-precomp 6.14] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> from Foundation import *
 >>> a = NSMutableArray.array()
 >>> d = NSMutableDictionary.dictionary()
 >>> a.addObject_("a")
 >>> a[0]
'a'
 >>> d['foo'] = 'bar'
 >>> d['baz'] = 'bob'
 >>> d.keys()
['baz', 'foo']
 >>> d.description()
'{baz = bob; foo = bar; }'
 >>> d.objectForKey_('baz')
'bob'
 >>> d['baz']
'bob'

Already in there -- just not quite complete.

> Does pyobjc do garbage collection?  I'd imagine that you could have 
> the __init__ for anything to a retain and the __del__ for anything do 
> a release without getting in the way..

Generally, you don't have to think about it (at least I haven't been 
and my apps aren't crashing -- they may be leaking like a sieve, though 
:-).  An assignment implies a -retain and destruction of the reference 
implies a -release.

It isn't all there yet, but I think we are fairly far down the correct 
path -- certainly far enough to write full featured apps with a minimum 
of pain.

BTW:  I also added this utility function to Foundation:

propertyListFromPythonCollection()

Which does this (in one of the examples in the pyobjc source):

Converting (Python Collection):
{'a': [1, 2, 3, 4],
  'b': 'c',
  'd': 3,
  'e': None,
  'f': [1, {'a': 'b', 2: 3}, [3, 4]],
  'g': {'h': 'i'},
  'j': (1, 2, 3, 'k', 'l')}


Converted (ObjC collection):
{
     a = (1, 2, 3, 4);
     b = c;
     d = 3;
     e = <null>;
     f = (1, {a = b; 2 = 3; }, (3, 4));
     g = {h = i; };
     j = (1, 2, 3, k, l);
}

b.bum