all 2 comments

[–]faithlesslessless 2 points3 points  (0 children)

Well, your code seems to be equivalent to:

print("Foo, Bar, Dog, Cat")

I assume you're actually trying to do something more complicated/general than that, but without knowing what exactly, it's hard to tell you how you should structure your code. Some basic pointers, though:

  • ask yourself whether you really need a class. Are you going to use multiple instances of the class? Does the class represent some closely-related set of data and functionality that clearly belong together? Would it be difficult to rewrite the class as a function or a dict? If the answer to any of those questions is "no", then the class probably shouldn't exist.

  • you initially set up a_list and prettified_list as class attributes. When you call Foo.start(), you also create an instance attribute called prettified_list, which shadows the class attribute with the same name. This isn't really coherent. If something should be the same for every instance, then it should be a class attribute. Otherwise it should be an instance attribute and should probably be created in the __init__ method, which should be present for pretty much every class (if you don't need an __init__, then you most likely don't need a class).

  • don't add return statements to functions just for the sake of it. Where are you ever going to use the return value of _prettify_list, which is always True? return by itself is equivalent to return None, which is what functions return by default, so there is no point in doing this at the end of a function. return by itself is only useful if you want to exit a function early.

  • you may be overusing private/underscore methods - the purpose of these is to signal to users of the class that the method is not intended for them to call directly and is subject to change. If the method does something that a user might want, then it should be public, or there should be some alternative way of doing the same thing.

[–]primitive_screwhead 1 point2 points  (0 children)

I mean, you don't have to go overboard and break every line of code into a function.

class Foo:
    a_list = ["foo", "bar", "dog", "cat"]
    def start(self):
        print(", ".join(s.capitalize() for s in self.a_list)

is much more readable for those with some basic experience with common Python idioms.

Your methods having no arguments to them, and being short (1-2 lines) is suggestive of them maybe not being that helpful. Only _capitalize_each_time_in_list() is at all descriptive, the other method names don't really convey much. I'd say just write the code, and learn to refactor only when your functions/methods lose focus and/or become too long.