473,387 Members | 3,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Jun 27 '08 #1
2 1310
On Sat, 07 Jun 2008 00:45:07 +0200, Stef Mientki
<st**********@gmail.comwrote:
>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
Jun 27 '08 #2
thanks guys,

David C. Ullrich wrote:
On Sat, 07 Jun 2008 00:45:07 +0200, Stef Mientki
<st**********@gmail.comwrote:

>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
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: harry | last post by:
I have an object that's passed in to a function as a parameter i.e public boolean getProjectTitle(ProjectHeader_DTO obj) {...} If I then call a method on this object inside the function i.e...
0
by: Ole | last post by:
Good day, perhaps someone is interested in enhancing this regular expression (^((\w+)(\-)?|(\.)?)+((\w+)((\s)+|((\.)(\s)*)))(\d+)) it is meant to filter a string, corresponding to a valid...
6
by: H | last post by:
This is a question that has haunted me for quite some time. if you build a 4 tier database application where the 4th tier is the database server (MS SQL 2000), where do you build the connection...
1
by: Al Dykes | last post by:
Does anyone have an idea on how to find ZIP+4 codes? Thanks -- a d y k e s @ p a n i x . c o m Don't blame me. I voted for Gore.
2
by: rcamarda | last post by:
I'm thinking of doing some basic parsing of address. I want to seprate out the house number from the street name from the suffix. I have the suffix's from the USPS, but I cant find a database of US...
2
by: =?Utf-8?B?Q29lbg==?= | last post by:
Hi, can anyone tell me 1) what is a easy way to build a xml fragment into a string. I don't what the fragment to be written to file, just in a string will be fine. I can build the xml fragment...
6
by: Arben | last post by:
Can anyone halp me to write the Java code for thid exercise? -------------------------------------------------------------------------------------------------------------- Exercise: Use the class...
0
by: Tommy Grav | last post by:
On Jun 6, 2008, at 6:45 PM, Stef Mientki wrote: This calls the int.__mul__() code i believe. If memory serves me right look into __rmul__(). Cheers Tommy
4
by: HowHow | last post by:
Using Access 2000. I need to group the address by suburb then by street name in my report. Two major problems here: 1. Unit number has the word "Unit" before number 2. Semi detach house has "a"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.