9 ms·
Ask HN: 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?
- hnthrowaway0315 5y agoI 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.
- als0 5y agoI 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.
- rdtwo 5y agoReally 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
- sys_64738 5y agoMost 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.
- cat_adorer 5y agoI'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.
- smorgusofborg 5y agoProbably. 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.
- viraptor 5y agoGenerally 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.
- d--b 5y agoNO! 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!)
- simonblack 5y agoLet 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!]
- hansvm 5y agoI'm very comfortable with both, and there are definitely still use cases where I'll reach for Python. E.g., Jax "just works" for a wide variety of numerical problems, handles the automatic differentiation and operator fusion and whatnot and figures out how to efficiently shove it onto whichever coprocessor(s) I want to target.
- raymondh 5y agoPython 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.
- smckk 5y agoNo. 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.
- Grismar 5y agoYes, 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.
- h2odragon 5y agoI 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.
- PotentialArt635 5y agoI 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.
- chris_st 5y agoI'd really rather recommend Nim [1] for performance-intensive apps. Its syntax resembles Python, it is far and away safer, but it complies (via generating C) to a full binary, and the resulting binary is very fast. Don't let the fact that it has a garbage collector scare you -- you can turn it off if you don't like it. 1: https://nim-lang.org/ https://nim-lang.org/
- Tozen 5y agoPython has a much stronger relationship to C than Nim. Thus many would argue it would be a better recommendation to learn C, if a Python user. In the case of Nim, it would arguably be a language to learn instead of Python, as oppose to somebody who is already an advanced Python user. Other strong choices of newer languages to choose from would be Golang, Rust, Red, or Vlang.
- jstx1 5y agoUnless 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.
- hprotagonist 5y agoabsolutely 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.
- WithinReason 5y agoWhat 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.
- kibwen 5y agoPython 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.
- medo-bear 5y agogiven that python is usually implemented in c (look up cpython), knowing c can help you at least understand internals of python
- lumb63 5y agoIn 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.
- mitch3x3 5y agoFor many people (myself included), the ease of writing python jumpstarted my journey to eventually learning everything else you mentioned gradually. If I would have had to learn it immediately I most likely would’ve felt disinterested and overwhelmed and given up since I didn’t know yet why I needed to learn it.
- tmjdev 5y agoThis is my major gripe with the college courses I took. They start at the molecular level and build to the planetary scale. This works for some people, but for me I had the hardest time finding a personal interest until I got to the application. It was the application I wanted to build, and to do actual work you don't manipulate the molecules directly. Build an application, then as you need to learn more to get things done you naturally pick up a deeper knowledge of the tech you're using. Of course this is different for everyone.
- andrewfromx 5y agoThink 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)
- kallisti5 5y agoYes. 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.
- oneplane 5y agoExactly. Learn multiple languages. Not really 'all of them' or all super in-depth, but if you want to do more than complete simple pre-cooked tasks of a to-do list (or JIRA board) you'll probably need to understand a bit more about the how/what/why. Learning more languages or different languages will let you see the similarities and differences but also the way they all end up running on the same system, calling into the same libraries and operating system. That said, languages are generally just 'ways to write', often aligned with 'ways to think' but they aren't themselves the algorithmic and data structural concepts which are very useful to at least know and have played around with a bit. It doesn't mean you actually end up re-implementing every doubly linked-list in all your code, but knowing the concept of hashing and binary trees, seeing how they can be helpful in hashmaps, and how turning arrays into trees and back again gets you so much extra understanding that when you do finally end up writing some production code and needing to make it work faster, use bigger datasets or reduce cost by optimising resource usage you'll at least know where to look and what to search for in your refactoring spree. Learning C and perhaps C++ is great for writing system libraries that need to run close to the metal, but I wouldn't really want programmers to still use those languages in general software development. If Go and Rust gets you there 90% of the way and you can glue things together with Python, Lua and other stuff you'll be able to make reasonable software for general use which is what most development ends up being anyway. Java (and C#) are still there of course, and you'll still want to at least play around with those just to get a sense of the (software world) around you. The time that you just boxed yourself in with 1 OS, 1 IDE and 1 Language with 1 Framework is passing (or has mostly passed) in commercial development. Throwing raw source code over the fence for 'ops' to do the rest is dying off.
- chris_st 5y agoI 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.
- joeberon 5y ago
- MichaelRazum 5y agoNot to understand Python. BUT if you need to write high performance code, it helps a lot.
- DarylZero 5y agoLOL I learned C before Python even existed
- amelius 5y agoYes, learn C. Then play with the ctypes Python module and invoke your C code from Python.
- qwertyuiop_ 5y agoOn 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.
- pyjarrett 5y agoNo, 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.
- AuthorizedCust 5y agoDo we need to learn assembler to understand C well enough? That’s a litmus test for this question.
- gpderetta 5y agoWell enough? No. To understand it better? Possibly. Whether it is true for C and Python I don't really have an opinion.
- deleted 5y ago[deleted]
- sebow 5y agoI 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.
- rightbyte 5y agoI don't think the mental step between C and assembler is that big. You got almost no constructs in C that does not map directly to assembler, like function overloading or classes. The confusion starts when the optimzer throws around your code and makes it unreadable.
- monkeybutton 5y agoI 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.
- medo-bear 5y agoare you saying that if you want to learn the internals of cpython you don't need to know c before hand? i think learning c is a must for this case
- monkeybutton 5y agoNo, I'm saying C is not enough on its own.
- medo-bear 5y agosure there is much more to it than just knowing c, but learning it should be encouraged as an important stepping stone if someone wants to learn cpython internals ... or even internals of many (most?) popular python libraries
- medo-bear 5y agohere 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
- bikingbismuth 5y agoI 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.
- ralgd 5y agoI 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.
- Lucasoato 5y agoDo 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.
- moggi 5y agoI can recommend https://realpython.com/products/cpython-internals-book/ https://realpython.com/products/cpython-internals-book/ as a decent initial documentation of cpython. I have a copy of this book at my desk for the few cases when I need to debug GIL or memory allocator related issues in cpython.
- throwawaynay 5y agoI 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.
- Shorel 5y agoI agree with you, and at the same time, I notice your username says throwaway. It seems liking the C language is frowned upon in this site.
- gaws 5y ago> if you learn C well, you're basically employable for the next 30 years. I think I agree with that. In embedded systems programming, right? It must take significant C expertise and experience to be gainfully employed. Maybe I'm overthinking it?
- abiro 5y agoOnce 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/ https://cs50.harvard.edu/x/2022/weeks/
- CraigJPerry 5y agoLearning 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.
- frigg 5y agoWould 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.
- cturtle 5y agoYes! 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!
- barisx 5y agoI 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.
- Tempest1981 5y agoA 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.
- gorgoiler 5y agoYes, 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 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.
- wirthjason 5y agoIt’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.
- mabbo 5y agoThe 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.
- mam4 5y agoThe best is both. And learning c++ python bindings
- quesomaster9000 5y agoLearn 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.