Skip to content

Instantly share code, notes, and snippets.

@broqdev
Last active December 10, 2015 12:18
Show Gist options
  • Select an option

  • Save broqdev/4433729 to your computer and use it in GitHub Desktop.

Select an option

Save broqdev/4433729 to your computer and use it in GitHub Desktop.

Revisions

  1. broqdev revised this gist Jan 2, 2013. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions derivedmethod.py
    Original file line number Diff line number Diff line change
    @@ -3,14 +3,16 @@ def __init__(self):
    self._func = None

    def callDerivedFunc(self):
    # self._func() # wrong
    # self._func() # call bound method
    # self._func(self) # call unbound method
    method = self._func
    method(self) # right
    method(self) # call unbound method

    class Bar(Foo):
    def __init__(self):
    super(Foo, self).__init__()
    self._func = Bar.hello
    self._func = Bar.hello # unbound method
    # self._func = self.hello # bund method

    def hello(self):
    print 'Hello'
  2. broqdev created this gist Jan 2, 2013.
    19 changes: 19 additions & 0 deletions derivedmethod.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    class Foo(object):
    def __init__(self):
    self._func = None

    def callDerivedFunc(self):
    # self._func() # wrong
    method = self._func
    method(self) # right

    class Bar(Foo):
    def __init__(self):
    super(Foo, self).__init__()
    self._func = Bar.hello

    def hello(self):
    print 'Hello'

    bar = Bar()
    bar.callDerivedFunc()