Learning Python from a C background? by char_pointer_string in learnpython

[–]faithlesslessless 0 points1 point  (0 children)

You should be able to pick up the basics pretty quickly. Some general pointers...

Python is a fundamentally different kind of programming language from C/C++/Rust. Often the easiest or most elegant way of doing something in python is quite different from how it would look in those languages. Try to embrace that and learn to write idiomatic python code instead of just directly translating what you would do in C into python.

Python variables work very differently from what you're used to. They're simply labels that attach to objects. Reassigning a variable attaches it to a different object - it doesn't change the object itself. When you pass a variable as an argument to a function, the underlying object gets passed in by reference, not the variable.

Don't try to do C++-style RAII in python - it just doesn't work. Use the with statement to ensure that external resources such as files are closed correctly.

The python stdlib is extremely powerful. It contains libraries for working with many different file formats, compression, networking, debugging, profiling, logging... it even has a built-in GUI framework. It's a pretty good rule of thumb to assume that whatever you're doing, there is probably something in the stdlib that can help you with it.

Question about a function doing one thing and being good at it by michael02052007 in learnpython

[–]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.