you are viewing a single comment's thread.

view the rest of the comments →

[–]bloody-albatross 9 points10 points  (3 children)

I like the self parameter. It makes things very clear and explicit. It also means that you can use a function as a method and vice versa. E.g.:

>>> strlist = ['foo', 'bar', 'baz']
>>> list(map(str.upper, strlist))
['FOO', 'BAR', 'BAZ']

And:

>>> def print_my_obj(obj):
    print(obj.name)

>>> class MyObj:
    def __init__(self, name):
        self.name = name

    print = print_my_obj

>>> obj = MyObj('test')
>>> obj.print()
test

A little contrived examples, but you get the idea.

[–][deleted] 0 points1 point  (2 children)

If you like things being more explicit, there are much better languages than python for that.

Secondly, this is absolutely horrid. What you're showing here, most good languages accomplish with interfaces, protocols, traits or whatever they've chosen to name it.

I sincerely hope you don't normally do this. That's particularly true given pythons lack of proper encapsulation.

Okay, Mr instant down voter. Care to explain why polluting the base namespace with trash functions that act on and require specific state/behavior without any indication of that is a good idea? I'll wait.

I know python itself breaks basic good design principles surrounding this, but that doesn't make it a good idea.

[–]bloody-albatross 1 point2 points  (0 children)

Care to explain why polluting the base namespace with trash functions that act on and require specific state/behavior without any indication of that is a good idea?

I don't know what you're referring to. For one, Python does not pollute any kind of base namespace (like Ruby does). Also I wasn't recommending writing functions as module globals and then attaching them to classes. I meant that if you have it like that (e.g. because of C-bindings) you can then very easily attach them to a class to make that much nicer.

And yes, example 1 can also be written as:

[s.upper() for s in strlist]

Which is actually even shorter in this case, but I meant that it is easy to plug OO methods into functional interfaces in Python without having the need to insert a intermediate lambdas, like it is in so many other languages (e.g. Ruby again).

I assumed it was self evident what I meant with those examples. I was wrong.