Argh!! Can't wrap my head around this Python stuff!

nospam.nospam.bartc bc at freeuk.com
Sat Nov 25 18:10:00 EST 2017


On 26/11/2017 09:09, Greg Tibbet wrote:
>
> I'm an old timer, have programmed in Fortran, C, C++, Perl, and a bit
> of Java and trying to learn this new-fangled Python language!
>
> I've got a small program that uses PIL to create an image, draw some
> primitives (rectanges, ellipses, etc...) and save it.  Works fine...
> no issues.
>
> I've found in the past, the best way to "really learn" the language
> was to "dig into the guts" and understand it,.. I thought I was making
> progress, but when looking into the PIL library to see what's going on
> behind the scenes, I find the following code in ImageDraw.py
>
> def ellipse(self, xy, fill=None, outline=None):
>          """Draw an ellipse."""
>          ink, fill = self._getink(outline, fill)
>          if fill is not None:
>              self.draw.draw_ellipse(xy, fill, 1)
> <...snipped...>
>
> ellipse() uses the method  self.draw.draw_ellipse()   Okay, fine...
> but WHERE is draw_ellipse defined??  What magic is happening there?
> I've searched the entire PIL directory tree, and the ONLY two places
> draw_ellipse is mentioned are right there in the ellipse() function...
> WHAT am I missing??

Python isn't a very pure language in that much of the functionality that looks
like it should be written in Python (because you imported a module just like
you import any Python module), actually is written in something else, as has
been pointed out.

It's reasonable that some things need to be implemented using some foreign
functions. But the boundary between Python and non-Python is blurred.

Take this program:

  import sys

and try and find sys.py in your installation.

(This is an obstacle if, for example, you're thinking of implementing a Python
interpreter. In theory, once you have it working, it should run any .py
program. But the critical modules it needs don't have .py source code. And the
interface to those non-Python functions isn't defined with special byte-code
instructions.

(It will be done /via/ those instructions, but the magic needed is on the other
 side of them. Calling into sys.fn() uses the same CALL_FUNCTION byte-code as
calling into a regular Python function.)

As I said, it's not pure. More of a jungle as you've found out.)

--
bartc




More information about the Python-list mailing list