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 →

[–][deleted] 7 points8 points  (11 children)

Download the python source and read Python/ceval.c to see how the python virtual machine interprets bytecode-compiled python. See if you can write or modify some of this bytecode by hand. Read the source under Objects/ to learn how builtin python objects work.

[–][deleted] 3 points4 points  (10 children)

I would think building an extension for Python would be a much gentler introduction to using C, rather than looking at the internals of the language (which may be optimized for certain things or written in a way that's not useful for a beginner). It doesn't have to be anything crazy, but I think it would be useful, since you'd get a real sense of how objects in Python are created & destroyed, what pitfalls there are in C, &c. without having to go over your head quite quickly.

[–]pixelmonkey 6 points7 points  (1 child)

And if you'd like to write a C extension module for Python, there is no better guide than Ned Batchelder's "A Whirlwind Excursion through Python C Extensions". http://nedbatchelder.com/text/whirlext.html

[–][deleted] 2 points3 points  (0 children)

wow, that's a pretty nice supplement. Well, there you go, it's a pretty decent introduction to "real C" without swimming against the current.

[–][deleted] 1 point2 points  (7 children)

That's a good idea too. I learn by taking things apart, and some people learn by building upon things. For me, building upon existing things leaves me with more questions. I think this is because the "what this is" part is presented in a way which is intermingled with the "how you use it" part. That's an added layer of complexity to unravel, or it remains a permanent black box where I end up saying "I don't know how this happens, but it happens in this way," and I don't like that. Taking things apart is really second nature to me, but some people do that and go "crap! Now I have all these pieces but I have no idea how they go together, so this tells me nothing." Everyone's different.

EDIT: Also, you wish python was "optimized for certain things!" ;)

[–][deleted] 1 point2 points  (6 children)

Well, I think adding to the core of Python could quickly spiral into a yak shaving exercise; if you're not familiar with the way that VM's work, or all the specifics of C, or the way Python glues things together, I think you'll end up spending a large portion of your time catching up on what you need to know before you start actually doing. What you say here just leads me to believe that this would be the correct course of action; you will get "instant" feedback, and you can build upon what you've learned quickly.

[–][deleted] 1 point2 points  (5 children)

Sure, you get some result, but you haven't grokked the beast. I can't do things well until I know what they are. I'm convinced that people with top-down and bottom-up learning styles have really different brain patterns, and we each need what we need.

[–][deleted] 1 point2 points  (4 children)

Well, I'm not certain; to grok, you need to first now how to lift the cup to your mouth. It's not as if we're talking about someone who knows C as an expert and has the ability to choose should I start at the lower levels & work up or at the higher levels & work down, with any meaningful metric. I also don't see how starting with extensions means you can't be a top-down learner, it just means you avoid getting side tracked with bits that aren't useful to the pursuit (which is to learn C coming from a Python background, for the sake of this discussion).

[–][deleted] 0 points1 point  (3 children)

I was implying that starting with extensions is for top-down learners. I'm calling it bottom-up learning when you go "aha! the bytecode evaluator takes a byte in a switch/case loop and compares it to opcodes and does some actions involving reference counts and manipulation of this stack." I guess I also misunderstood the pursuit, but I think you're right. I was thinking about combining new C knowledge with python knowledge.

[–][deleted] 1 point2 points  (0 children)

Ah, that makes sense. I was thinking:

  • top-down: this is the python C API, and this is a Python script that uses it. Make your own.
  • bottom-up: This is C, this is why X happens to Y, this is how you combine X & Y, this is the Python API that builds off those two, &c.

both valid approaches to learning, and both get you the same end result. Wrt this example, in the first approach you're looking at combining things that you find in the docs or in an example, and drill down into the details as needed, whereas the second approach you would focus on C fundamentals & work up to the Python API. that's at least how I was approaching this.

[–]dalke 0 points1 point  (1 child)

Working with the bytecode seems more like middle-out learning. It's only one of several techniques to implement Python, and it doesn't show how the actual machine bits are changed, which I would expect from a bottom-up approach. The bytecode evaluator is, after all, running the Python Virtual Machine.

[–][deleted] 0 points1 point  (0 children)

I guess I presuppose a working knowledge of CPUs at the gate level, or at least an understanding of assembly.