Re: [Tutor] class arguments?

2009-01-23 Thread Kent Johnson
Forwarding to the list with my reply...

On Fri, Jan 23, 2009 at 1:35 PM, spir  wrote:
> Le Fri, 23 Jan 2009 06:45:04 -0500,
> Kent Johnson  a écrit :
>
>> On Fri, Jan 23, 2009 at 6:04 AM, spir  wrote:
>>
>> > 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.
>>
>> A simple way to do this is with a class factory function, for example:
>>
>> def makeMonoList(typ, number):
>>   class MonoListSubtype(MonoList):
>> item_type = type
>> item_number = number
>>   return MonoListSubtype
>
> That's it! Stupid me!! [Just realize I have a kind of mental blocage that 
> prevents me *imagining* a class beeing defined inside a func. As for me a 
> class is a higher level kind of thing. Actually I also have problems with 
> defs insides defs. Maybe there should be more introduction to that in python 
> literature. Probably it may help and simplify a whole lot of models.]
> Thank you again.
>> then e.g.
>> IntegerList = makeMonoList(int, 5)
>> myIntegerList = IntegerList()
>>
>> This is similar in spirit to collections.namedtuple() in Python 2.6
>> though the implementation is different; namedtuple() actually creates
>> and evaluates the text of the new class definition:
>> http://docs.python.org/library/collections.html#namedtuple-factory-function-for-tuples-with-named-fields
>> http://svn.python.org/view/python/trunk/Lib/collections.py?rev=68853&view=auto
>
> I have watched that some time ago -- as you pointed to it already. I take the 
> opportunity to ask why this contruct is so complicated. There is an 
> alternative in the cookbook (also pointed by Kent, if I remmember well) that 
> is only a few lines long. Something like:
>
> class Record(dict):
>def __init__(self,**kwargs):
>dict.__init__(self,kwargs)
># and/or
>self.__dict__ = kwargs
>
> [There are several versons around, +/- based on the same principle]

namedtuple() creates a new class that has exactly the desired
attributes, so it is a bit more specific and typesafe - you have to
have the correct number of points. The generated class subclasses
tuple so it can be used as a dict key (if the items themselves can
be). Class instances are lightweight because they don't have a
__dict__ member.

> Actually, the thing I like at least in the namedtuple recipe is that it 
> writes the class def as a string to be executed:

Yeah, I know...with all the time we spend on the list telling people
not to use eval()...

>
> I know there are several advantages:
> * a docstring
> * For large collections of records of the same (sub)type, as the list of 
> field is held  by the class (instances record the actual data only), which 
> spares memory. But doesn't this lead to lower performance, as attribute 
> access by name requires adressing a class level attribute?

The attributes are properties, so attribute access is like a method
call, I suppose this is slower than direct field access but it is a
common Python technique.

> * attributes can be accessed by index, too
>
> Also, as this factory create kinds of records, meaning data constructs with 
> an identical structure, that would perfectly hold table records, why isn't it 
> simply called "record".
> To sum up in a word: why so much *complication*?

I guess you would have to search comp.lang.python or python-dev to
find the reasons, I don't think there is a PEP for this (at least not
referenced in the What's New).

Kent
>
> Denis
> --
> la vida e estranya
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] class arguments?

2009-01-23 Thread Kent Johnson
On Fri, Jan 23, 2009 at 6:04 AM, spir  wrote:

> 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.

A simple way to do this is with a class factory function, for example:

def makeMonoList(typ, number):
  class MonoListSubtype(MonoList):
item_type = type
item_number = number
  return MonoListSubtype

then e.g.
IntegerList = makeMonoList(int, 5)
myIntegerList = IntegerList()

This is similar in spirit to collections.namedtuple() in Python 2.6
though the implementation is different; namedtuple() actually creates
and evaluates the text of the new class definition:
http://docs.python.org/library/collections.html#namedtuple-factory-function-for-tuples-with-named-fields
http://svn.python.org/view/python/trunk/Lib/collections.py?rev=68853&view=auto

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] class arguments?

2009-01-23 Thread spir
Le Thu, 22 Jan 2009 23:29:59 -,
"Alan Gauld"  a écrit :

> 
> "Alan Gauld"  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


Re: [Tutor] class arguments?

2009-01-22 Thread Alan Gauld


"Alan Gauld"  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?



Yes thats what the __init__ method is for.

class MonoList:
  def __init__(self, lst, typ, num):
  self.item_type = typ
  self.number = num
 etc...

myList = MonoList([1,2,3], int, 3)# or whatever...


Alan G.

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


Re: [Tutor] class arguments?

2009-01-22 Thread Kent Johnson
On Thu, Jan 22, 2009 at 5:18 PM, Kent Johnson  wrote:
> On Thu, Jan 22, 2009 at 4:51 PM, spir  wrote:
>> Hello,
>>
>> is there a way to give arguments to a class definition? Eg
>>
>> class MonoList(list, typ, number):
>>item_type = typ
>>item_number = number
>
> Use the type() function (which is the constructor for the type 'type')
> to dynamically create classes (i.e. types), e.g.
>
> In [1]: typ = int
>
> In [2]: number = 3
>
> In [3]: MonoList = type("MonoList", (list,), dict(item_type=typ,
> item_number=number))
>
> In [4]: ml = MonoList()
>
> In [5]: ml.item_type
> Out[5]: 
>
> In [6]: ml.item_number
> Out[6]: 3
>
> In [7]: isinstance(ml, list)
> Out[7]: True

And to show that these are class attributes:
In [4]: MonoList.item_number
Out[4]: 3

In [5]: MonoList.item_type
Out[5]: 

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] class arguments?

2009-01-22 Thread Kent Johnson
On Thu, Jan 22, 2009 at 4:51 PM, spir  wrote:
> Hello,
>
> is there a way to give arguments to a class definition? Eg
>
> class MonoList(list, typ, number):
>item_type = typ
>item_number = number

Use the type() function (which is the constructor for the type 'type')
to dynamically create classes (i.e. types), e.g.

In [1]: typ = int

In [2]: number = 3

In [3]: MonoList = type("MonoList", (list,), dict(item_type=typ,
item_number=number))

In [4]: ml = MonoList()

In [5]: ml.item_type
Out[5]: 

In [6]: ml.item_number
Out[6]: 3

In [7]: isinstance(ml, list)
Out[7]: True

http://docs.python.org/library/functions.html#type

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] class arguments?

2009-01-22 Thread Alan Gauld


"spir"  wrote 

is there a way to give arguments to a class definition? Eg

class MonoList(list, typ, number):
item_type = typ
item_number = number


Yes thats what the __init__ method is for.

class MonoList:
  def __init__(self, lst, typ, num):
  self.item_type = typ
  self.number = num
 etc...

myList = MonoList([1,2,3], int, 3)# or whatever...

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

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


[Tutor] class arguments?

2009-01-22 Thread spir
Hello,

is there a way to give arguments to a class definition? Eg

class MonoList(list, typ, number):
item_type = typ
item_number = number

[I guess you understand what I try to do...]

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