0

I am new in Python and I wrote the following code:

class Frazione:
    def __init__(self, Numeratore, Denominatore=1):
        mcd=MCD(Numeratore,Denominatore)
        self.Numeratore=Numeratore/mcd
        self.Denominatore=Denominatore/mcd

    def MCD(m,n):
        if m%n==0:
            return n
        else:
            return MCD(n,m%n)

    def __str__(self):
        return "%d/%d" %(self.Numeratore, self.Denominatore)

    def __mul__(self, AltraFrazione):
        if type(AltraFrazione)==type(5):
            AltraFrazione=Frazione(AltraFrazione)
        return Frazione(self.Numeratore*AltraFrazione.Numeratore, self.Denominatore*AltraFrazione.Denominatore)

    __rmul__=__mul__

Open shell at the same folder of Frazione.py:

>>> from Frazione import Frazione 

end then

>>> f=Frazione(10,5)

When I press Enter, I receive this output:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".\Frazione.py", line 5, in __init__
  mcd=MCD(Numeratore,Denominatore)
  NameError: global name 'MCD' is not defined

PS. I apologize for my english!

3
  • 3
    Note that it is recommended that you write your code with English identifiers. This makes getting help easier, and allows more people to read your code more easily. (Reference: python.org/dev/peps/pep-0008/.) Commented Jun 1, 2013 at 13:54
  • Are you looking for fractions.Fraction that comes with python? Commented Jun 1, 2013 at 14:08
  • I'm sorry for identifiers! I just copy and paste my code. Regarding the problem it was an exercise, so I don't care about fractions.Fraction. Commented Jun 1, 2013 at 22:46

1 Answer 1

5

MCD is a method of Frazione, but you're calling it as if it were a global function. The easiest (and cleanest, IMHO) fix is to just move it outside the class, because it doesn't need to access any class or instance members.

So:

def MCD(m, n):
    if m % n == 0:
        return n
    else:
        return MCD(n, m % n)

class Frazione:
    # as before but without MCD

If you do want to keep it in the class, then you might rewrite it to be iterative instead of recursive and call it as self.MCD in __init__. That's a good idea anyway, as Python's support for recursion is rather weak.

Sign up to request clarification or add additional context in comments.

3 Comments

Can you elaborate on "Python's support for recursion is rather weak"? Are you referring to the fact that function calls are relatively slow in Python?
@EOL: there is no tail call elimination, and the BFDL doesn't want it either. Then there's the speed issue, the single global depth limit (which can be reset, of course) and the fact that exception tracebacks can grow very large when using deep recursion, making debugging hard. The "flat is better than nested" maxim applies to algorithms as well as data structures.
If I move MCD outside the class it works. Thanks! But I don't know why, when I tried before to post this question, it didn't work. (:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.