[Patches] [ python-Patches-477330 ] add __slot_docs__ and __slot_types__

noreply@sourceforge.net noreply@sourceforge.net
Fri, 02 Nov 2001 16:13:07 -0800


Patches item #477330, was opened at 2001-11-01 15:08
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=477330&group_id=5470

Category: Core (C code)
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Michael McLay (mclay)
Assigned to: Nobody/Anonymous (nobody)
Summary: add __slot_docs__ and __slot_types__

Initial Comment:
The slot members defined using__slots__  cannot be
assigned doc strings in the current implementation. 
This patch adds the ability to attach doc strings to a
slot member by assigning a dictionary to the
__slot_docs__ class variable.

>>> class B(object):
    __slots__ = ['a','b','c']
    __slot_docs__ = {'a':"the a docstring",'b':"the b
doc string"}
    __slot_types__ = {'a':(int,str), 'c':int, }

>>> B.a.__doc__
'the a docstring'
>>> 

The __slot_types__ class variable adds an optional type
constraint for the slot.  If a dictionary with a type
or list of types is associated with a member name then
the isinstance() method will be called on that member
when the get and set methods for the member are called.

>>> class B(object):
    __slots__ = ['a','b','c']
    __slot_docs__ = {'a':"the a docstring",'b':"the b
doc string"}
    __slot_types__ = {'a':(int,str), 'c':int, }
    def __getattribute__(self,name):
	    print "help please", name
	    return object.__getattribute__(self, name)

	
>>> class B(object):
    __slots__ = ['a','b','c']
    __slot_docs__ = {'a':"the a docstring",'b':"the b
doc string"}
    __slot_types__ = {'a':(int,str), 'c':int, }

>>> b = B()
>>> b.a
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in ?
    b.a
TypeError: The value of B.a is of type <type
'NoneType'>. This is not one of the defined slot_types
>>> b.a = 4
>>> b.a
4
>>> b.a = 34.3
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in ?
    b.a = 34.3
TypeError: The type of <type 'float'> is not one of the
defined slot_types for B.a
>>> b.b
>>> b.b = 3
>>> b.b = 34.2
>>> b.b
34.200000000000003
>>> b.a = "fish"
>>> b.a
'fish'
>>> 

----------------------------------------------------------------------

>Comment By: Martin v. Löwis (loewis)
Date: 2001-11-02 16:13

Message:
Logged In: YES 
user_id=21627

-1. I think there should be proper syntax for slots, which 
should include support for doc strings. The "optional 
static typing" part should be coordinated with PEP 245.


----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=477330&group_id=5470