Is it worth it to learn C to better understand Python?
For those who learned C after learning Python, was it worth it? Did it help you to better understand Python?
In college I used to tutor students in computer science. Mid-way through my college career my school switched the curriculum to teach Python instead of Java. I found that the students who took Python lacked what I considered a basic appreciation for and understanding of how the language actually ‘does’ something.
For instance, when you only need to write two curly braces to create a “dictionary” (of course behind the scenes this is a hash table) many of the nuances of that data structure are hidden from you. You have no idea that accesses are O(1) amortized but worst case are O(n). Even if you do, maybe you don’t understand why. You have no idea that there is reallocation and collision handling code working behind the scenes constantly.
Python also lacks a number of nuances I consider it important to be aware of. You live your entire Python life blissfully unaware that everything is a pointer, and the impact that has on performance. You never come across static typing, or that it is possible to check your code for some level of syntactic correctness before it runs (i.e. Python is interpreted rather than compiled).
I think from this standpoint, it is worth learning a language like C to gain an appreciation for how truly complex a lot of the things Python allows you to do, are. I think that anything which helps you better understand what it is you are doing, how you are doing it, and why you are doing it that way, will make you better at doing that thing.
I think every coder who have the time to do so should learn C. Preferably before anything else.
Maybe I'm biased by my education, since I started with almost a year of hardcore C(almost no libs, coding everything from scratch), I did code a bit before but nothing serious.
After 1 year of segfaults, memory leaks, and random buffer overflows, everything else seems pretty easy, and everything make much more sense.
You won't just get better at python, you'll get better at pretty much anything involving programming or understanding how a computer works.
I've seen a lot of people say that if you learn C well, you're basically employable for the next 30 years. I think I agree with that.
Yes. Learn C.
Not to understand python, but to become a better programmer and gain a better understanding of computers.
We desperately need more people learning the 'hard' languages. Python is fun, but libssl, libcurl, the Linux kernel, and all the other 'important' things need more developers.
No, it won't help you better understand Python.
However, for professional development, you should probably learn it anyways before you jump into other lower level languages.
C is the Latin of programming languages, as it's the common root of a flurry of languages, and you see it's influence in a lot of places. Even though it's not exactly "how a computer works," understanding its purpose and problems puts other languages into context.
I learnt C around the same time when I was learning Python and I don't believe it had much of an impact on the latter. Unless you're building a Python interpreter, I don't see how it can help with learning the language. While C has served me and many others well, I don't think it should come before learning a modern alternative like Rust or Go.
Yes, but in my experience it will teach you more about the layer beneath (the machine) than it will about the layer above (Python.)
The technology we call “C” is not just the language — it’s the whole system of language, preprocessor, compiler, linker, assembler and debugger that comes together to produce and inspect machine code. In terms of computing machinery, there is no more fundamental target than this.
Accordingly, you should learn enough about the C ecosystem to be able to debug a C program. It’s enough to be able to read code in C, but you will want to write some to get a feeling for the culture (passing the address of where you want the result of a function as a function argument is the big one, and it’s accompanying subtasks around memory management and pointers.)
You don’t have to go inside the compiler itself but I would recommend looking at some simple machine code and trying to assemble some yourself. You could do this on arm or x86, or on an emulator like this one for high school students: https://www.peterhigginson.co.uk/AQA/info.html
Once you build a sufficiently complex bit of assembly code you’re going to want to extract out common “functions” and give them names, and develop a convention for how to use memory, which registers do what when you call a function.
That’s what a C compiler actually does.
NO!
You should learn some more fundamental aspects of programming:
1. What's the stack, what's the heap, why do I get a stack overflow from recursion?
2. In C, what's a pointer, how does it relate to arrays?
3. What's garbage collection, and how does it work?
And that will eventually lead you to the inevitable:
4. Why is Python so slow? (haha - getting ready for the downvotes!)
It’s not clear what is meant by “to better understand Python”. Better understand CPython, e.g. and you want to hack on the interpreter, or better understand how to write more creative/abstract/safe Python?
In my experience Python programmers can be divided into two groups, those who write Python and those who write Python frameworks, aka Python for other Python programmers.
The latter group is trying to create an abstraction that helps solve a problem in an elegant way. Often to do that a deep understand of the language is needed such as the use of descriptors, higher order functions, the Python object model, metaclasses.
One of the best books for this is “Fluent Python”. It’s also a good to browse around significant projects and understand how they work and what patterns are used, eg Django, Flask, etc.
Python is so far removed from C that I struggle to think of anything about C that would help you to better write or understand Python. If anything, it might make your Python worse if you try to import C idioms to Python.
If what you really want is to write or read C, then of course you should learn C.
Unless you want to contribute to CPython, no.
What about Python are you trying to understand? You can learn more about Python internals without needing too much C. And you can get better at Python without knowing much about the internals or C. So it looks like a waste of time.
No. Would you then learn assembly to understand C better?
Once you have the basics of syntax and semantics your next step is the OOP and FP paradigms used in Python for simple programs.
Then a deep dive for a solid grasp of data structures and algorithms would be the last step.
After you have these 3, you should be able to write mid to high level programs comfortably.
Which of these 3 aspects do you feel C would help you understand better?
Maybe you are asking this question because you want to understand programming languages and how they work in which case a course in compilers and programming languages from a MOOC would help.
Most Python programmers will struggle with the lack of comparable functionality in the standard C lib. How do you tell a Python programmer that in C to use sets or a hash or a dictionary, that you need to implement them yourself or find a third party library? Also, how do you explain the C compile-link-run process to a Python programmer who only knows that Python just runs? A lot of basics are missing in C and in Python Programmers.
Maybe guide them to C++ for standard template library equivalence. But then you have another ocean of pain because C++ is the kitchen sink.
On a long enough timeline even dynamic languages want static types. Looking at you JS/ Typescript. Yes learning C gives you an appreciation of how types “as simple as” a String in Python is constructed.
I never got too far into C, but I took a course on assembly and learned Erlang and Golang to a decent proficiency. Erlang gave me an appreciation of loops and recursion, Golang made me more aware of types and error handling, and assembly just made me happy that I don’t have to write assembly. All this of this made me a significantly better Python programmer and made me appreciate what was going on under the covers a bit more. I also find value in challenges like advent of code, because it gives me a chance to tinker with a language in new ways.
Would you learn how to fly a plane to better understand how a car works? The answer is no of course. You learn more of Python and its internal workings to understand Python better.
Python instructor and C programmer here:
No need to learn C to better understand Python. Almost any gain you would make could be had by reading or watching an overview of the internals.
Yes! Or at least a language that gives you access to manual memory allocations.
I learned python, JavaScript, and Java before C, and it wasn’t until I learned about manual allocations, pass-by-value, and pointers that the garbage collected languages made sense. It had never really clicked why all object params are references in those languages until I learned C and C++
Now, I don’t think it’s essential, but learning more about how computers and memory operate doesn’t hurt!
Once you start hitting Python’s limits, understanding how the interpreter works (which is implemented in C) will be of great help.
You could take weeks 1-5 of CS50 [1] to learn enough C to get by. The course continues from week 6 with Python so it’s the perfect intro.
[1] https://cs50.harvard.edu/x/2022/weeks/
Learn both at the same time :)
But, the twist is start from a higher level algebraic problem, like a generalized N-dimensional distance function.
Get started with enough Python to do it in two dimensions, then translate the core of your algorithm to Cython and compare the benchmarks. This will get you up to speed with the build processes progressing towards pure C in comparison to plain old interpreted Python.
Are you using floats or integers? How accurate is it compared to using a calculator etc. as the numbers get much bigger or have many more numbers after the decimal point.
Then, what about the median distance of two arrays of 2d coordinates? Again, benchmark, try out some different approaches to the problem in both languages and see where the different strengths lie, wrapping your head around how programs interface with each other internally across domains - because that's what it's all about right?
Now... what about 3 dimensions? Or four?
I hope you see where I'm going here, if you're looking for a fundamental understanding then you need to get into the nitty gritty - albeit with a conceptually very straightforward problem.
I think unironically learning assembly helped me the most in understanding the computer itself,I don't know about understanding Python through this manner.But through this fashion you can get an idea of how functions are made and operate.I feel like the jump between asm and C & 'higher' languages is bigger than the one between C and Python.
What do you want to understand about Python? To understand the performance of Python it's more important to understand how CPUs work, but C doesn't help much there. If you look up how e.g. a hash table is implemented at the low level it should give you and idea how the "standard library" and dicts of Python work.
I learned C and have worked with C++ professionally in the past and that knowledge doesn't help at all with understanding what Python is doing internally. Sure, CPython is written in C, but that fact doesn't tell you anything about the details of how it's implemented or what it does when it runs.
The point of learning C as a python developer isn't to learn C nearly as much as it is to better understand what's going on in the levels of abstraction below.
Many years ago, as a student, a friend let me borrow their copy of "Hacking: The Art of Exploitation". At the time I thought I was going to be learning about security. What I learned instead was what a program was, what a computer was, at a surprising level of detail.
Especially the early chapters, a true picture of what computers are doing is given to the reader. And once you understand that, you could apply the knowledge to security, no doubt. But you could also apply it to understanding many higher level languages- Python included.
You can find older editions online as PDF files. Really recommend it.
Do we need to learn assembler to understand C well enough? That’s a litmus test for this question.
A friend just finished a beginning Scheme + Java class, and asked me: How are languages like C++, golang, Rust, Lisp, Scala, and Python different.
Any pointers to a good video on the topic? "Overview/Survey of features in programming languages"? Thanks.
Yes, because learning C will teach you about computing and how your computer works. However C is not the only language that could teach you and you don't have to become fluent in it to understand enough to help your Python. Learning enough about a few languages (i.e being able to write something in them) will help you appreciate what Python does for you behind the scenes, but also how Python limits what you can do with it. Ultimately, if you master Python, you may want to write some packages for it that aren't written in Python and then C is a sensible choice.
Yes, learn C. Then play with the ctypes Python module and invoke your C code from Python.
Probably. If you should look at the python implementation and the standard libraries it uses and the kernel you are using they are all primarily in C and in rare cases of other languages they are setup with FFI that favors the thought process of a C programmer.
When I look at a language like python or PHP I often just go down to the C code they call and use a tracer to watch calls to solve a problem without actually using the higher language.
I advocate learning ASM to understand the machine; C and python are the same relation one step of abstraction up. So yes, more knowledge of C and how Python is written and what the interpreter is doing with your code is very likely to be helpful.
Interfacing with python with your own C code is easy and the combination of the two is a much more powerful tool than either alone.
I would say it depends on what you want to do.
I would choose Python for fast prototyping and proofing-of-concepts, C for performance-intensive apps.
Let me ask that same question from the other direction:
If you're a (reasonably proficient) C programmer, is learning to use Python worth the effort? Or is it just redundant education?
I can see that learning Python is probably worth it for a new programmer, but is it worth it for a proficient C programmer?
[Let the flame wars begin!]
I think it's more about the projects you work on than the language you use. C is a system programming language and most appropriate projects are compilers, operating system, classic console programming, embedded system programming and other low level stuffs.
I think you should learn nim programming language against C. Yes, because nim can exporting C code when you write python syntax like nim language. Not all of them, also you can port as javascript and C++. Just try it out. Easy to learn, better way to write C and absolutely worth.
Do you know any source to better study how python works? I mean something which are the steps to understand the internal CPython bindings and so on?
I really want to step-up my Python skills but this part seems really hard for people with a non CS related background.
I'd actually recommend learning a higher-level language, like Common Lisp, to better understand Python: knowing about the relevant algorithms and how they transfer should do far more to educate you than learning C, which shares no abstractions with Python.
Think about how many fundamental Linux programs are still done in C. Yes. 100%. Knowing C well helps you in any higher level language. In your mind you’ll be able to think what is really happening under the abstraction that is python. (Or ruby or Java etc)
Not to understand Python. BUT if you need to write high performance code, it helps a lot.
here is a way knowing c can help you become a better "python" programmer: write a library that is much faster in c than in python (allways the case). then create a python wrapper around it and make it a python library. something like this happens with all numerical libraries in python
besides that, knowing other languages will help you become a better programmer in general. usual classic advice is learn c, lisp, and ml
however you still need to know good programming practices, data structures, and algorithms. with the exception of the first, these are not language specific
I learned C after learning Python. It broadened my horizon, I grew dissatisfied with Python and now work in C++ and Go. So it was worth it, though not in the sense that you have probably intended.
absolutely not.
you might be able to grep cpython source more intelligently, but it won't help you at all with the semantics of the python language itself (versus its dominant implementation), and might hurt.
Learning Python’s dis and inspect modules would be more valuable first IMO. Then ctypes then cython. All seem more useful to me than just C from a python programmer perspective.
Generally learning X to be a better programmer in Y works for any X and Y languages. There's always something that you can learn and transfer to your daily language.
I think it will make you appreciate how much Python does for you, and (particularly if you try to do anything large) how many more bug-types are possible in C.
LOL I learned C before Python even existed
Really the op should ask if learning c will make him better at leetcode. That’s really the metric all programming salaries are judged by it seems
The best is both. And learning c++ python bindings