Comments on: Ruby style method injection in Python https://lizards.opensuse.org/2009/04/29/ruby-style-method-injection-in-python/ Blogs and Ramblings of the openSUSE Members Fri, 06 Mar 2020 17:50:09 +0000 hourly 1 https://wordpress.org/?v=4.7.5 By: Nicolas https://lizards.opensuse.org/2009/04/29/ruby-style-method-injection-in-python/#comment-933 Thu, 30 Apr 2009 23:28:33 +0000 http://lizards.opensuse.org/?p=890#comment-933 You’d get unbound method if you tried to call the method as if it were a static method, i.e. straight off the class … in this case Foo.bar().

if that is indeed what you wanted to do… you have to make it a class method … (by either @classmethod in python 2.4+ or simply bar = classmethod(bar) after its definition in the class)

]]>
By: Michal Vyskocil https://lizards.opensuse.org/2009/04/29/ruby-style-method-injection-in-python/#comment-932 Thu, 30 Apr 2009 09:48:10 +0000 http://lizards.opensuse.org/?p=890#comment-932 I thought I got something like unbound method error in this case, but your example runs well.

]]>
By: Joachim Werner https://lizards.opensuse.org/2009/04/29/ruby-style-method-injection-in-python/#comment-931 Thu, 30 Apr 2009 08:21:28 +0000 http://lizards.opensuse.org/?p=890#comment-931
class Foo:
def foo(self):
return ‘foo’

aFoo = Foo()
aFoo.foo()

def bar(self):
return ‘bar’

Foo.bar = bar

aFoo.bar()

]]>
By: Joachim Werner https://lizards.opensuse.org/2009/04/29/ruby-style-method-injection-in-python/#comment-930 Thu, 30 Apr 2009 08:19:18 +0000 http://lizards.opensuse.org/?p=890#comment-930

class Foo:
def foo(self):
return ‘foo’

aFoo = Foo()
aFoo.foo()

def bar(self):
return ‘bar’

Foo.bar = bar

aFoo.bar()

]]>
By: Joachim Werner https://lizards.opensuse.org/2009/04/29/ruby-style-method-injection-in-python/#comment-929 Thu, 30 Apr 2009 08:15:19 +0000 http://lizards.opensuse.org/?p=890#comment-929 Another try. Seems like the code tag is not what makes my code snippet show up as code:

]]>
By: Joachim Werner https://lizards.opensuse.org/2009/04/29/ruby-style-method-injection-in-python/#comment-928 Thu, 30 Apr 2009 08:10:18 +0000 http://lizards.opensuse.org/?p=890#comment-928 What about this?


class Foo:
def foo(self):
return 'foo'

aFoo = Foo()
aFoo.foo()

def bar(self):
return 'bar'

Foo.bar = bar

aFoo.bar()

It does just the same as the initial Ruby example, but IMHO makes it much more obvious that I’m adding a new method to an existing class.

]]>
By: Simon Davy https://lizards.opensuse.org/2009/04/29/ruby-style-method-injection-in-python/#comment-927 Thu, 30 Apr 2009 07:25:35 +0000 http://lizards.opensuse.org/?p=890#comment-927 hmm – both languages allow excactly the same thing with different syntax – python is more explicit, ruby is more implcit, as per normal

]]>
By: Michal Vyskocil https://lizards.opensuse.org/2009/04/29/ruby-style-method-injection-in-python/#comment-926 Thu, 30 Apr 2009 07:21:36 +0000 http://lizards.opensuse.org/?p=890#comment-926 Interesting, I didn’t tested it with class (and static) methods, because I don’t need it, so thanks for the link.

]]>
By: Michal Vyskocil https://lizards.opensuse.org/2009/04/29/ruby-style-method-injection-in-python/#comment-925 Thu, 30 Apr 2009 07:20:20 +0000 http://lizards.opensuse.org/?p=890#comment-925 I don’t think that this is a best example, because this implicit behavior of open classes is against Python design rules. Better example should be in method naming, when method? returns boolean and method! modify an instance. In Python is obviously not clear which method do what. So in this case is Ruby more “pythonic” (means explicit) than Python itself :).

]]>
By: Michal Vyskocil https://lizards.opensuse.org/2009/04/29/ruby-style-method-injection-in-python/#comment-924 Thu, 30 Apr 2009 07:06:47 +0000 http://lizards.opensuse.org/?p=890#comment-924 I like Python philosophy “explicit is better than implicit”. So those implicit adding of new methods which looks like normal class declaration* is uncommon. At least for those of us which are not familiar with Ruby. And I am, because that was my first “hello world” in Ruby :-).

* Yes, I know that definitions/declaration does not make a sense in dynamic languages, but I’m still thinking in those sentences.

]]>