Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

how to build a street with more than 1 house ?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


stef.mientki at gmail

Jun 6, 2008, 3:45 PM

Post #1 of 4 (184 views)
Permalink
how to build a street with more than 1 house ?

hello,

In the code below, I can build a large street like this:
large_street = house * 25
but not a small street. like this:
small_street = 5 * house

Why is this different ?
And more interesting, how do I get the right results ?

thanks,
Stef Mientki

class type_house ( object ) :
def __init__( self, front_doors = 1 ) :
self.front_doors = front_doors
def __mul__ ( self, b ) :
return type_house ( self.front_doors * b )

house = type_house ()
large_street = house * 25
print large_street.front_doors
small_street = 5 * house
print small_street.front_doors

--
http://mail.python.org/mailman/listinfo/python-list


tgrav at mac

Jun 6, 2008, 3:48 PM

Post #2 of 4 (175 views)
Permalink
Re: how to build a street with more than 1 house ? [In reply to]

On Jun 6, 2008, at 6:45 PM, Stef Mientki wrote:

> hello,
>
> In the code below, I can build a large street like this:
> large_street = house * 25
> but not a small street. like this:
> small_street = 5 * house

This calls the int.__mul__() code i believe.

> Why is this different ?
> And more interesting, how do I get the right results ?

If memory serves me right look into __rmul__().

Cheers
Tommy
--
http://mail.python.org/mailman/listinfo/python-list


dullrich at sprynet

Jun 7, 2008, 6:48 AM

Post #3 of 4 (157 views)
Permalink
Re: how to build a street with more than 1 house ? [In reply to]

On Sat, 07 Jun 2008 00:45:07 +0200, Stef Mientki
<stef.mientki [at] gmail> wrote:

>hello,
>
>In the code below, I can build a large street like this:
> large_street = house * 25
>but not a small street. like this:
> small_street = 5 * house
>
>Why is this different ?

Because you're multiplying on the left in one
case and on the right in the other.

You realize that house*5 should work, right?

>And more interesting, how do I get the right results ?
>
>thanks,
>Stef Mientki
>
>class type_house ( object ) :
> def __init__( self, front_doors = 1 ) :
> self.front_doors = front_doors
> def __mul__ ( self, b ) :
> return type_house ( self.front_doors * b )

The reason house*25 works is that the __mul__
method says what it should be. If you want
5*house to work you need a __rmul__.
Nothing in Python automatically makes
a*b the same as b*a; when it sees 5*house
first it checks 5 to see whether it knows
whether it knows what 5*house should be
(no, 5 never heard of this house thing), then
it checks house to see if it knows what
5*house should be (no, house has no
__rmul__).

The simplest thing is just to define __rmul__
to make multiplication commuttative:

def __rmul__(self, b):
"""Defines what b*self should return."""
return self*b

Now 5*house calls __rmul__, which
returns house*5. That in turn calls __mul__,
which returns what you want. And some day
when you modify __mul__ (in a subclass?)
you won't need to worry about making the
same change to __rmul__.

>house = type_house ()
>large_street = house * 25
>print large_street.front_doors
>small_street = 5 * house
>print small_street.front_doors

David C. Ullrich
--
http://mail.python.org/mailman/listinfo/python-list


stef.mientki at gmail

Jun 7, 2008, 2:21 PM

Post #4 of 4 (159 views)
Permalink
Re: how to build a street with more than 1 house ? [In reply to]

thanks guys,

David C. Ullrich wrote:
> On Sat, 07 Jun 2008 00:45:07 +0200, Stef Mientki
> <stef.mientki [at] gmail> wrote:
>
>
>> hello,
>>
>> In the code below, I can build a large street like this:
>> large_street = house * 25
>> but not a small street. like this:
>> small_street = 5 * house
>>
>> Why is this different ?
>>
>
> Because you're multiplying on the left in one
> case and on the right in the other.
>
> You realize that house*5 should work, right?
>
>
yes.
>> And more interesting, how do I get the right results ?
>>
>> thanks,
>> Stef Mientki
>>
>> class type_house ( object ) :
>> def __init__( self, front_doors = 1 ) :
>> self.front_doors = front_doors
>> def __mul__ ( self, b ) :
>> return type_house ( self.front_doors * b )
>>
>
> The reason house*25 works is that the __mul__
> method says what it should be. If you want
> 5*house to work you need a __rmul__.
> Nothing in Python automatically makes
> a*b the same as b*a; when it sees 5*house
> first it checks 5 to see whether it knows
> whether it knows what 5*house should be
> (no, 5 never heard of this house thing), then
> it checks house to see if it knows what
> 5*house should be (no, house has no
> __rmul__).
>
> The simplest thing is just to define __rmul__
> to make multiplication commuttative:
>
Aha, now a bell begins to ring ...
> def __rmul__(self, b):
> """Defines what b*self should return."""
> return self*b
>
this works indeed ...
> Now 5*house calls __rmul__, which
> returns house*5. That in turn calls __mul__,
> which returns what you want. And some day
> when you modify __mul__ (in a subclass?)
> you won't need to worry about making the
> same change to __rmul__.
>
>
thanks for the clear explanation.

cheers,
Stef
>> house = type_house ()
>> large_street = house * 25
>> print large_street.front_doors
>> small_street = 5 * house
>> print small_street.front_doors
>>
>
> David C. Ullrich
> --
> http://mail.python.org/mailman/listinfo/python-list
>

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.