Le Thu, 22 Jan 2009 23:29:59 -0000,
"Alan Gauld" <alan.ga...@btinternet.com> a écrit :

> 
> "Alan Gauld" <alan.ga...@btinternet.com> wrote 
> 
> >> is there a way to give arguments to a class definition? 
> 
> I see that Kent interpreted your question differently to me. 
> If you do mean that you want to dynamically define class 
> attributes rather than instance attributes then __init__() 
> won't work. But I'd be interested to understand why and 
> how you would want to do that? And in particular how 
> you would use them after creating them?

Thank you Alan and sorry for not having been clear enough. The point actually 
was class (definition) attributes. I thought at e.g. Guido's views that lists 
were for homogeneous sequences as opposed to tuples rather like records. And a 
way to ensure sich a homogeneity, in the sense of items beeing of the same type 
or super type.
The straightforward path to ensure that, as I see it, is to add proper argument 
to a class definition. But I couldn't find a way to do this. In pseudo-code, it 
would look like that:

class MonoList(list, item_type):
        typ = item_type
        def __init__(self,items):
                self._check_types(items)
                list.__init__(self,items)
        def _check_types(self,items):
                for item in items:
                        if not isinstance(item,MonoList.typ):
                                message = "blah!"
                                raise TypeError(message)
        def __setitem__(self,index,item):
                if not isinstance(item,MonoList.typ):
                        message = "blah!"
                        raise TypeError(message)
                list.__setitem__(self,index,item)
        def __add__(self,other):
                self._check_types(other)
                list.__add__(self,other)
        .......

Well, I realize now that it is a bit more complicated. MonoList itself should 
be an intermediate base class between list and and subclasses that each allow 
only a single item type. Otherwise all monolist-s have the same item type ;-)
Just exploring around...

denis

> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


------
la vida e estranya
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to