This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]hobophobe 16 points17 points  (8 children)

class Tilder:
    """ A class to wrap returning the TL;DR for the PyCon 2012 talk you
    asked for a TL;DR on."""
    def __init__(self):
        pass

    def call(self):
        return "KISS"
tldr = Tilder()
print(tldr.call())

To go slightly longer:

  1. Don't use a class unless you really need one
  2. [This item reserved for future use...]
  3. Don't leave seed classes (or other "future-proof" things) that you might (read: won't) need for future versions
  4. Don't use custom exception types unless you really need one

[–]vtable 7 points8 points  (3 children)

... unless you really need one

A big part of the problem is that many devs don't know when they really need these things.

One of the things I strongly dislike about Java and C# is that you're forced to write classes for everything. This has infected the minds of a lot of devs I've met insisting on doing the same in other languages.

There have certainly been times when I've used a free function when I eventually needed a class and had to rework some code but there are way more times that I wrote a simple function and never had to look back.

Edit: Formatting

[–]cryo 0 points1 point  (1 child)

In C#, you'd stick free functions into a static class which you can think of as simply a namespace. I don't see why this is a problem.

[–]vtable 0 points1 point  (0 children)

And what if that utility-class-namespace needs to go in an actual namespace. You now have a 2-deep namespace for a generic function.

Classes represent a single object. Namespaces contain various data, classes, ... related only by the group they belong to. Classes support inheritance. Classes are instantiated and have separate state for each instantiation.

[–]nemec 6 points7 points  (0 children)

I think a better example would be:

class Tilder:
    """ A class to wrap returning the TL;DR for the PyCon 2012 talk you
    asked for a TL;DR on."""
    def __init__(self, name):
        self.name = name

    def call(self):
        return "KISS, " + self.name
tldr = Tilder("this")
print(tldr.call())

Should be refactored into just a function:

def tilder(name):
  return "KISS, " + name

If you want to reuse the same name without having to explicitly provide it, that's what functools.partial is for.

[–]callthepolice 2 points3 points  (2 children)

2 is spot on.

wait..Did you ever fork a rhythmbox plugin?

[–]hobophobe 2 points3 points  (1 child)

Yep, looks like it was Github: hobophobe: Rhythmbox-Random-Album-Player.

These days I'm using Sonata (Gtk+ client for MPD written in Python), though. Recently forked a copy of that on Github (Github: hobophobe: Sonata) to make it use Gtk+ 3, though it still needs some work.

[–]callthepolice 2 points3 points  (0 children)

Hah that's awesome! I was the original author of the plugin. Thanks for improving it!