Python Forum
When to decide whether or not to have an argument in a class method?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
When to decide whether or not to have an argument in a class method?
#1


I'm not sure on an appropriate title - I hope the one given fits.
I have the following code:

class House():

    def __init__(self, roof, side):
        self.roof = roof
        self.side = side

    def volume(self):
        return self.side ** 3

    def price(self, otherSide):
        if otherSide.volume() < self.side.volume():
            return "The house is more expensive" 

myHouse = House(True, 25.5)
print myHouse.volume()
print myHouse.price(18.5)
Explanation of class
Here I am creating an instance of a House, defining it on whether it has a flat roof or not, and then on its side length, assuming that it is a perfect cube, not including the area of the attic, if it has one.

I define a volume method to compute the volume of the house.

I define a price method to see if one house is smaller than another by comparing the volumes, and then if the house is smaller it is cheaper.

Initial error
This gives me the following problem:

Error:
The volume of the house is: 69455.9011819 cubic metres. Traceback (most recent call last): File "try2.py", line 18, in <module> print theHouse.price(18.5) File "try2.py", line 13, in price if otherSide.volume() < self.radius.volume(): AttributeError: 'float' object has no attribute 'volume'
So the code works fine for finding the volume of a house when using a particular house as the object of the volume attribute. However, it breaks down when I want to calculate the volume of two houses at once.

I think that the issue here is that I am defining the volume method as only using the instance side length to calculate the volume (so it will only ever work for the instance House).

Attempt to solve the problem
So I attempted to solve this problem by putting an additional argument into the volume method, like so:

def volume(self, side2):
    return side2 ** 3
This also throws up an error, which is likely because I am just creating the opposite problem (the method now only works on a side length taken in the argument, and not the instance House as an object).

The error is:

Error:
Traceback (most recent call last): File "try2.py", line 17, in <module> print theHouse.volume() TypeError: volume() takes exactly 2 arguments (1 given)
Ultimately I just want to be able to write some code that allows me to use the price method to compare the sizes of two Houses.

Any suggestions on how I can edit my code to do this?
Reply
#2
to the particular error in your code (the first one) you should amend the code as follows
 
    def price(self, otherSide):
        if otherSide**3 < self.volume():
            return "The house is more expensive" 
 
however there are many flaws in your code. some of them:
- first of all price method will return None if the volume of other house has bigger/same volume
- method name (price) is misleading. one would expect it to return/calculate price. more appropriate name should be used
- it's more logical to take as argument another house object, not side size
Reply
#3
(Oct-29-2017, 03:36 PM)buran Wrote: to the particular error in your code (the first one) you should amend the code as follows
 
    def price(self, otherSide):
        if otherSide**3 < self.volume():
            return "The house is more expensive" 
 
however there are many flaws in your code. some of them:
- first of all price method will return None if the volume of other house has bigger/same volume
- method name (price) is misleading. one would expect it to return/calculate price. more appropriate name should be used
- it's more logical to take as argument another house object, not side size

Thanks. Could you explain the change to self.volume() instead from self.side.volume(). I have no doubt that it works, but I don't understand why the original way does not work.

(Oct-29-2017, 03:43 PM)JoeB Wrote:
(Oct-29-2017, 03:36 PM)buran Wrote: to the particular error in your code (the first one) you should amend the code as follows
 
    def price(self, otherSide):
        if otherSide**3 < self.volume():
            return "The house is more expensive" 
 
however there are many flaws in your code. some of them:
- first of all price method will return None if the volume of other house has bigger/same volume
- method name (price) is misleading. one would expect it to return/calculate price. more appropriate name should be used
- it's more logical to take as argument another house object, not side size

Thanks. Could you explain the change to self.volume() instead from self.side.volume(). I have no doubt that it works, but I don't understand why the original way does not work.

Actually I think I know why. When I call theHouse.price() I am initialising theHouse as 'self'. Thus you simply have to use self.volume() and the method knows that the self is the side length of the house. Is that correct?
Reply
#4
you have a class House. it has properties - side, roof, volume.
i.e. self.side is a property. self.volume() is also a property (method to be precise). there is no such property self.side.volume().
self.side is actually just a floating number (i.e. float object, not House object). you need to read/revisit class basics. https://python-forum.io/Thread-Classes-Class-Basics
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  super() and order of running method in class inheritance akbarza 7 594 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Using one child class method in another child class garynewport 5 1,484 Jan-11-2023, 06:07 PM
Last Post: garynewport
  i want to use type= as a function/method keyword argument Skaperen 9 1,771 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,267 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  class, attribute and method Frankduc 9 2,379 Feb-27-2022, 09:07 PM
Last Post: deanhystad
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,679 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,657 Oct-30-2021, 11:20 PM
Last Post: Yoriz
Question How to pass a method as argument in an another method? anilanvesh 6 2,663 Sep-30-2021, 10:18 PM
Last Post: deanhystad
  anonymous method in a class Skaperen 8 3,475 May-23-2021, 11:17 PM
Last Post: Skaperen
  How to apply a class method to an entire dataframe column tirtha9 1 5,051 Jan-03-2021, 04:44 AM
Last Post: klllmmm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020