0

I'm starting with the OOP. I have a class which uses @staticmethod. This calls methods form other class.

My Code:

@staticmethod
def import_plane(self, matriculation):

    try:
        plane_file = '{}{}{}'.format(
            paths.planes, matriculation.upper(),
            project.aircraft_ext)

        with open(plane_file, 'r') as plane_file:
            reader = json.load(plane_file)

            plane = Plane.Plane(
                reader['Matriculation'],
                reader['Type'],
                reader['OACI'],
                reader['Colors'],
                self._get_speeds(reader),
                self._get_fuel(reader),
                self._get_runway(reader),
                reader['RDBA'],
                reader['Transponder'],
                reader['Turbulance'],
                reader['Certification'],
                self._get_equipments(reader),)

    except Exception as exception:
        return str(exception)

    else:
        return plane


def _get_speeds(self, reader):
    return {
        'crusing':    reader['Speeds']['Crusing'],
        'climb':      reader['Speeds']['Climb'],
        'vz_climb':   reader['Speeds']['VzClimb'],
        'descent':    reader['Speeds']['Descent'],
        'vz_descent': reader['Speeds']['VzDescent'],
        'vso':        reader['Speeds']['VSO'],
        'vfe':        reader['Speeds']['VFE'],
        'vno':        reader['Speeds']['VNO'],
        'vne':        reader['Speeds']['VNE'],
        'vx':         reader['Speeds']['Vx'],
        'vy':         reader['Speeds']['Vy']
    }

Error: The method _get_speeds() should be static

I am using PyCharm IDE

Tilak Madichetti
  • 4,110
  • 5
  • 34
  • 52
Louis Etienne
  • 1,302
  • 3
  • 20
  • 37
  • first of all, why is your static method taking "this" as a parameter? static methods are class level methods that **should not** have access to this (object level variable)...lastly, static methods should ideally call other static methods and not instance level methods, thats why you get the issue on _get_speeds...finally consider using classmethod instead of staticmethod.... – labheshr Oct 24 '15 at 08:32
  • `except Exception as exception: return str(exception)` - **never do that**. You're not actually addressing the problem the exception indicates. You're just turning your nice, informative exception into some other weird error that'll occur after this nonsense return value has propagated through your code. If your function can't do anything about an exception, it shouldn't catch the exception. – user2357112 Oct 24 '15 at 08:40

1 Answers1

3

The _get_speeds method doesn't make use of the self variable (the instance of the class) anywhere in its scope -- that's why it's "static". You could easily enough call this method without instantiating the class:

MyClass._get_speeds(reader)

Normal instance methods can only work on a class-instance - in fact your import_plane method makes use of self and should not be a staticmethod.

This question and the top answer are well worth reading.

Community
  • 1
  • 1
Leo
  • 1,077
  • 11
  • 24