howto overload with a NOP (empty statement)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stef Mientki

    #1

    howto overload with a NOP (empty statement)

    How should I overload / disable a method ?
    In the example below I have defined the class "Power_Supp ly", derived
    from baseclass "device".
    The baseclass has a method "execute", which will be implemented in most
    derived classes, but not in all.
    Now apparently it's not allowed to overload a method with an empty
    statement.
    I could write a nonsense dummy statement, like "A= 3", but isn't there
    another way ?

    thanks, Stef Mientki

    class device:
    def execute (self):
    print 'execute not yet implemented for', self.Name


    class Power_Supply (device):
    def execute (self): ;
  • Vasily Sulatskov

    #2
    Re: howto overload with a NOP (empty statement)

    class Power_Supply (device):
    def execute (self):
    pass

    Stef Mientki wrote:
    How should I overload / disable a method ?
    In the example below I have defined the class "Power_Supp ly", derived
    from baseclass "device".
    The baseclass has a method "execute", which will be implemented in most
    derived classes, but not in all.
    Now apparently it's not allowed to overload a method with an empty
    statement.
    I could write a nonsense dummy statement, like "A= 3", but isn't there
    another way ?
    >
    thanks, Stef Mientki
    >
    class device:
    def execute (self):
    print 'execute not yet implemented for', self.Name
    >
    >
    class Power_Supply (device):
    def execute (self): ;

    Comment

    • Robert Kern

      #3
      Re: howto overload with a NOP (empty statement)

      Stef Mientki wrote:
      How should I overload / disable a method ?
      In the example below I have defined the class "Power_Supp ly", derived
      from baseclass "device".
      The baseclass has a method "execute", which will be implemented in most
      derived classes, but not in all.
      Now apparently it's not allowed to overload a method with an empty
      statement.
      I could write a nonsense dummy statement, like "A= 3", but isn't there
      another way ?
      pass

      --
      Robert Kern

      "I have come to believe that the whole world is an enigma, a harmless enigma
      that is made terrible by our own mad attempt to interpret it as though it had
      an underlying truth."
      -- Umberto Eco

      Comment

      • Bruno Desthuilliers

        #4
        Re: howto overload with a NOP (empty statement)

        Stef Mientki a écrit :
        How should I overload / disable a method ?
        In the example below I have defined the class "Power_Supp ly", derived
        from baseclass "device".
        <off>
        Naming conventions are to use CamelCase for class names. So it would be
        better to name your classes 'PowerSupply' (no '_') and 'Device'. You're
        of course free to use whatever naming convention you want, including no
        convention at all, but Python relies *a lot* on naming conventions...
        </off>
        The baseclass has a method "execute",
        <off>
        Do you know that Python let you define your own 'callable' objects ?

        class SomeCallable(ob ject):
        def __init__(self, name):
        self.name = name
        def __call__(self):
        print "wow, %s has been called" % self.name

        foo = SomeCallable('f oo')
        foo()

        This may or not make sens in the context of your application, but
        whenever you find yourself naming a method 'execute', it might be worth
        asking yourself if the object should in fact be a callable...
        </off>
        which will be implemented in most
        derived classes, but not in all.
        >
        Now apparently it's not allowed to overload a method with an empty
        statement.
        I could write a nonsense dummy statement, like "A= 3", but isn't there
        another way ?
        the 'pass' statement

        def noop():
        pass
        thanks, Stef Mientki
        >
        class device:
        def execute (self):
        print 'execute not yet implemented for', self.Name
        The usual idiom for 'pure virtual methods' is to raise a
        NotImplementedE rror. Now if it's ok for a subclass to implement the
        method as a no-op, why not just implement it as a no-op in the base
        class itself ?

        Comment

        • robert

          #5
          Re: howto overload with a NOP (empty statement)

          Stef Mientki wrote:
          How should I overload / disable a method ?
          In the example below I have defined the class "Power_Supp ly", derived
          from baseclass "device".
          The baseclass has a method "execute", which will be implemented in most
          derived classes, but not in all.
          Now apparently it's not allowed to overload a method with an empty
          statement.
          I could write a nonsense dummy statement, like "A= 3", but isn't there
          another way ?
          >
          thanks, Stef Mientki
          >
          class device:
          def execute (self):
          print 'execute not yet implemented for', self.Name
          >
          >
          class Power_Supply (device):
          def execute (self): ;



          class device:
          def execute (self):
          raise NotImplementedE rror('virtual: should do this and that')



          This NotImplementedE rror virtual method scheme will also be detected by pychecker in order to warn correctly: that classes of actual instances have to override all open virtuals.


          Robert

          Comment

          • Jussi Salmela

            #6
            Re: howto overload with a NOP (empty statement)

            Bruno Desthuilliers kirjoitti:
            Stef Mientki a écrit :
            >How should I overload / disable a method ?
            >In the example below I have defined the class "Power_Supp ly", derived
            >from baseclass "device".
            >
            <off>
            Naming conventions are to use CamelCase for class names. So it would be
            better to name your classes 'PowerSupply' (no '_') and 'Device'. You're
            of course free to use whatever naming convention you want, including no
            convention at all, but Python relies *a lot* on naming conventions...
            </off>
            Continuing with the style and idioms issues...

            1. Don't leave a space between a function/method/class name and the
            opening parenthesis, i.e. instead of:
            def execute (self):
            use:
            def execute(self):

            2. It seems to me that you indent your code with 2 spaces. If I'm wrong,
            please ignore me. Otherwise: always use 4 spaces when indenting with spaces.

            Following these and the numerous other guidelines (see
            htpp://www.python.org/dev/peps/pep-0008/ for example) on Python style
            you'll be making yourself and others a favor. When these coding
            guidelines become your habit, it will be easier for you to read the code
            of other Pythoners. This is important because a large part of learning
            consists of reading the code of others.

            And also vice versa: when you show your code to others, it will be
            easier for them to concentrate on your problem when the idiosyncrasy of
            your code is not there to hinder.

            Cheers,
            Jussi

            Comment

            Working...