1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How can I call a function within a class?

Discussão em 'Python' iniciado por Stack, Setembro 16, 2025.

  1. Stack

    Stack Membro Participativo

    I have this code which calculates the distance between two coordinates. The two functions are both within the same class.

    However, how do I call the function distToPoint in the function isNear?

    class Coordinates:
    def distToPoint(self, p):
    """
    Use pythagoras to find distance
    (a^2 = b^2 + c^2)
    """
    ...

    def isNear(self, p):
    distToPoint(self, p)
    ...


    Currently I get a NameError:

    Traceback (most recent call last):
    File "path/to/file.py", line 14, in <module>
    Coordinates().isNear((1, 2))
    File "path/to/file.py", line 10, in isNear
    distToPoint(self, p)
    NameError: name 'distToPoint' is not defined

    Continue reading...

Compartilhe esta Página