0

I want to create a method that only takes objects of the class it is is defined in, manipulates it and returns a new object of the same class.

For example:

class fraction():
    def__init__(self, counter, denominator):
        self.counter=counter
        self.denominator=denominator
    
    def add(self,(object of class fraction))
        (addition of object the method from called in and the object which is called in the  method
        return (new object of the class fraction)

Does anyone have an idea how to get this done? best regards, David

wwii
  • 23,232
  • 7
  • 37
  • 77
davmel
  • 65
  • 5
  • [Formatting help](https://stackoverflow.com/editing-help)... [Formatting sandbox](https://meta.stackexchange.com/questions/3122/formatting-sandbox) – wwii Jul 10 '21 at 14:38
  • You can also implement the class' special methods, such as `__add__()`, so that you can use operators with your class. – Pippet39 Jul 10 '21 at 14:39
  • The only way to do this is to check the argument in the function/method before trying to use it - then you have to decide what to do if it is not the correct type, like raise a ValueError. – wwii Jul 10 '21 at 14:43
  • Does [How do Python functions handle the types of the parameters that you pass in?](https://stackoverflow.com/questions/2489669/how-do-python-functions-handle-the-types-of-the-parameters-that-you-pass-in) answer your question? – wwii Jul 10 '21 at 14:48

1 Answers1

1

You can do something like this :

class Fraction:
    def __init__(self, counter, denominator):
        self.counter = counter
        self.denominator = denominator

    def add(self, fraction):
        return Fraction(self.counter * fraction.denominator + fraction.counter * self.denominator,
                        self.denominator * fraction.denominator)


a = Fraction(2, 3)
b = Fraction(3, 5)
a_plus_b = a.add(b)
print(a_plus_b.counter, a_plus_b.denominator)

You can add type checking directly in the add function :

def add(self, fraction):
        if not isinstance(fraction, Fraction):
            raise TypeError(f"{fraction} is not a Fraction.")
        return Fraction(self.counter * fraction.denominator + fraction.counter * self.denominator,
                        self.denominator * fraction.denominator)

If you want to integrate this in the standard fractions module, you can make a class inheriting from fractions.Fraction, which will outpute you a nice irreductible fraction :

import fractions


class MyFraction(fractions.Fraction):

    def add(self, fraction):
        return MyFraction(self.numerator * fraction.denominator + fraction.numerator * self.denominator,
                        self.denominator * fraction.denominator)


a = MyFraction(2, 3)
b = MyFraction(10, 6)
a_plus_b = a.add(b)
print(a_plus_b.numerator, a_plus_b.denominator)
o---o
  • 51
  • 4
  • Thank you very much!Two more questions: 1.) It works great but it does not check if the argument is the right type. How can I check if the argument is an object of the class Fraction? 2.) Can I combine this with the Fraction module? – davmel Jul 10 '21 at 14:51
  • You can add type checking directly in the add function – o---o Jul 10 '21 at 14:59
  • (1/2) Since Python is dynamically typed, there's no concept of restricting a function to have specific argument types. Arguments can be *any* type; it's the function's job to determine how to *use* them. Think of it as a form of implicit overloading: the same `add` function can handle `a.add(Fraction(3,5)` and `a.add(5)`, by checking at runtime if `b` is another `Fraction` or an `int`. – chepner Jul 10 '21 at 15:32
  • (2/2) That said, you can use type hints and `mypy` to check *statically* if you are writing code that passes the "wrong" type of argument at compile time. (For example, `a.add("foo")`.) – chepner Jul 10 '21 at 15:33