0

I'm creating a class with this method that computes for the centroid of a given area from coordinates. This is my code so far. And I'm having this error in jupyter notebook : NameError: name '_Lot__getCentroid' is not defined. What am I doing wrong? I'm trying to just follow this UML given to me. Should I move the method getCentroid first before the initialization? I'm not sure that's customary with classes but I'm new to it so I don't know. UML

rilke-nz
  • 33
  • 1
  • 6
  • 1
    you dont need to use staticmethod for private functions, use self in function declaration and function calling – sahasrara62 Apr 25 '21 at 19:15
  • @sahasrara62 i thought "@staticmethod" allows me to make a function that has no self as an argument? I was trying to follow the uml. Or am i misinterpreting the UML as well? – rilke-nz Apr 25 '21 at 19:34
  • Can a static method can be called on an instantiated object ?? – pippo1980 Apr 25 '21 at 19:53

1 Answers1

1

There two reasons why your code will not work:

  1. self.centroid = __getCentroid() will work only if __getCentroid() function will be declared outside the class. If it's inside the class, you should call self.__getCentroid()
  2. __getCentroid() is a static method and has no access to class or instance attributes. So you don't have access to self.*something*. If you use IDE such a Pycharm, it will show you the error.

Just read a little bit more about classes, their attributes and methods in python :)

Dmitry
  • 146
  • 3
  • If I remove the staticmethod, can I make the getCentroid function without the argument "self" ? Or is it necessary since i want to access the vertices in the initialization? – rilke-nz Apr 25 '21 at 19:36
  • And btw thank you! Do you have any recommendations on what I can read for classes? The ones I've seen so far have basic examples that don't show about private functions. – rilke-nz Apr 25 '21 at 19:38
  • Yes, you need to have `self` argument to access attributes. And looking at your code I don't see any problems in that, because you are already working with class object. About what to read - try this (about different methods) https://realpython.com/instance-class-and-static-methods-demystified/ – Dmitry Apr 25 '21 at 19:59