From: wheres pythonmonks on 29 Jul 2010 14:12 Why is the default value of an int zero? >>> x = int >>> print x <type 'int'> >>> x() 0 >>> How do I build an "int1" type that has a default value of 1? [Hopefully no speed penalty.] I am thinking about applications with collections.defaultdict. What if I want to make a defaultdict of defaultdicts of lists? [I guess my Perl background is showing -- I miss auto-vivification.] W
From: Paul Rubin on 29 Jul 2010 14:18 wheres pythonmonks <wherespythonmonks(a)gmail.com> writes: > How do I build an "int1" type that has a default value of 1? > [Hopefully no speed penalty.] > I am thinking about applications with collections.defaultdict. You can supply an arbitary function to collections.defaultdict. It doesn't have to be a class. E.g. d = collections.defaultdict(lambda: 1) will do what you are asking.
From: wheres pythonmonks on 29 Jul 2010 14:35 Thanks. I presume this will work for my nested example as well. Thanks again. On Thu, Jul 29, 2010 at 2:18 PM, Paul Rubin <no.email(a)nospam.invalid> wrote: > wheres pythonmonks <wherespythonmonks(a)gmail.com> writes: >> How do I build an "int1" type that has a default value of 1? >> [Hopefully no speed penalty.] >> I am thinking about applications with collections.defaultdict. > > You can supply an arbitary function to collections.defaultdict. > It doesn't have to be a class. E.g. > > d = collections.defaultdict(lambda: 1) > > will do what you are asking. > -- > http://mail.python.org/mailman/listinfo/python-list >
From: Nick Raptis on 29 Jul 2010 14:43 On 07/29/2010 09:12 PM, wheres pythonmonks wrote: > How do I build an "int1" type that has a default value of 1? You mean something like: >>> x = int() >>> x 0 >>> def myint(value=1): .... return int(value) .... >>> myint() 1 >>> That's ugly on so many levels.. Anyway, basic types (and almost everything else) in Python are classes. You can always subclass them to do whatever you like > [Hopefully no speed penalty.] > I am thinking about applications with collections.defaultdict. > What if I want to make a defaultdict of defaultdicts of lists? [I > guess my Perl background is showing -- I miss auto-vivification.] > > > Ah, python is no perl. Then again, perl is no python either. ----- Random pseudo-Confucius quote Have fun, Nick
From: John Nagle on 29 Jul 2010 15:28
On 7/29/2010 11:12 AM, wheres pythonmonks wrote: > Why is the default value of an int zero? > >>>> x = int >>>> print x > <type 'int'> >>>> x() > 0 >>>> > > How do I build an "int1" type that has a default value of 1? >>> class int1(object) : .... def __init__(self) : .... self.val = 1 .... def __call__(self) : .... return(self.val) .... >>> x = int1() >>> x() 1 This isn't useful; you'd also have to define all the numeric operators for this type. And then there are mixed-type conversion issues. Inheriting from "int" is not too helpful, because you can't assign to the value of the base class. "self=1" won't do what you want. [Hopefully no speed penalty.] In your dreams. Although all numbers in CPython are "boxed", so there's more of a speed penalty with "int" itself than you might expect. There are some C libraries for handling large arrays if you really need to crunch numbers. John Nagle |