15

I know that Python is mainly slower than languages like fortran and c/c++ because it is interpreted rather than compiled.

Another reason I have also read about is that it is quite slow because it is dynamically typed, i.e. you don't have to declare variable types and it does that automatically. This is very nice because it makes the code look much cleaner and you basically don't have to worry too much about variable types.

I know that there won't be a very good reason to do this as you can just wrap eg. fortran code with Python, but is it possible to manually override this dynamicly typed nature of Python and declare all variable types manually, and thus increasing Python's speed?

Jonny
  • 1,270
  • 5
  • 19
  • 31
  • 3
    Why is speed more important to you than readability in this case? Readable code can easily be *made* faster, but it can be very difficult to make fast code more readable. – Sam Estep Jul 05 '15 at 17:50
  • 7
    You can use Cython, using slightly altered Python syntax that compiles to C. But otherwise, making Python statically typed would basically make it a different language altogether. If you need that kind of speed, just use a statically typed language. – Martijn Pieters Jul 05 '15 at 17:50
  • If you read the whole question you will see that it is more a hypothetical one to do with the nature of Python than with computing speed @RedRoboHood – Jonny Jul 05 '15 at 17:52
  • Thank you @MartijnPieters, makes sense. – Jonny Jul 05 '15 at 17:54
  • 3
    @Jonny OK, but my question still remains. The dynamic nature of Python follows from some of the *key* aspects of its philosophy. If you want to use a statically typed language, you should just use a statically typed language. – Sam Estep Jul 05 '15 at 17:55
  • You should look into Boo, which is as you describe. – Malik Brahimi Jul 05 '15 at 17:56
  • Boo looks interseting. @RedRoboHood, I suppose the reason I'm asking is because I was wondering if there was a middle ground between fast but 'ugly' languages and slow but 'pretty' languages. Not a very good thing to winder about looking at the downvotes )-: – Jonny Jul 05 '15 at 18:00
  • 1
    @Jonny No, that's actually a very good thing to reason about. You're just going it about it the wrong way. Rather than asking, "How can I change Python so it's not Python anymore?" you should ask, "What languages are out there that lie somewhere between Python and [insert 'ugly' language here]?" – Sam Estep Jul 05 '15 at 18:03
  • 1
    @Jonny If you're interested in speed I'd look at pypy. For such a high level language as python it does a VERY good job of making it competitive with C. – CrazyCasta Jul 05 '15 at 18:03
  • 2
    I think you pretty much said it yourself in your question - most people just use Python as a convenient way to ship pointers around and do any heavy lifting in C/C++/Fortran, so there's not actually all that much to be gained from making it faster. I think that's one of the main reasons why PyPy still hasn't really taken of, despite it being much faster than CPython in certain use cases. – ali_m Jul 05 '15 at 18:10
  • 1
    I think you are missing the point about dynamically typed languages - they are not the same as *untyped* languages. In python objects are typed, but "variables" are untyped references, a bit like a `void *` (in the C implementation, a `PyObject *`) where the object it points to knows what type it is. It's objective is flexibility, not laziness. – cdarke Jul 05 '15 at 18:14
  • Python is slow at low-level stuff but you can use stuff like numpy, sum, and, all, product, and itertools to implement your low-level loops in terms higher-level loops to make your code faster. – aoeu256 Jul 30 '19 at 17:59

2 Answers2

14

If I interpret your question as "Is there a statically-typed mode for Python?", then Cython probably comes closest to offering that functionality.

Cython is a superset of Python syntax - almost any valid Python code is also valid Cython code. The Cython compiler translates the quasi-Python source code to not-for-human-eyes C, which can then be compiled into a shared object and loaded as a Python module.

You can basically take your Python code and add as many or as few static type declarations as you like. Wherever types are undeclared, Cython will add in the necessary boilerplate to correctly infer them, at the cost of worse runtime performance. This essentially allows you to choose a point in the continuum between totally dynamically typed Python code and totally statically typed C code, depending on how much runtime performance you need and how much time you are prepared to spend optimizing. It also allows you to call C functions directly, making it a very convenient way to write Python bindings for external libraries.

To get a better idea of how this works in practice, take a look at the official tutorial.

ali_m
  • 71,714
  • 23
  • 223
  • 298
  • Thank you @ali_m. How does pypy (that is mentioned in a few comments above) compare to Cython? Does the JIT compiler also translate to C as in Cython? – Jonny Jul 05 '15 at 18:47
  • 2
    I don't know the exact implementation details of PyPy's JIT compiler, but I'm pretty sure it compiles directly to native machine code without any intermediate C code step. The main drawback with PyPy is compatibility - support for major numerics libraries such as numpy, scipy etc is patchy to nonexistent, which outweighs its benefits for most people who really care about performance. See here for more discussion: http://stackoverflow.com/q/18946662/1461210. – ali_m Jul 05 '15 at 19:01
  • In Cython even we have the option to statically declare variables with a python Type, so you don't need to go outside of Python. Here's a great video series about Cython on Youtube: https://youtu.be/-nXrJmI5JjU?sub_confirmation=1 – Abdul Rehman Jan 11 '21 at 07:30
1

Just to be clear your question is just about as odd as asking if you can turn C into a dynamically typed language. If you want to redefine the language, then sure, you can do whatever you like. I don't think we'd call such a language "Python" anymore though.

If you're looking for speed up based on a dynamic static typing (static typing that is dynamically found) implementation of the language take a look at pypy. It's also quite fast if that's what you're looking for. Related to pypy is RPython which sort of does what you want.

Also mentioned previously is Cython which sort of does what you want.

CrazyCasta
  • 26,917
  • 4
  • 45
  • 72
  • 1
    *I don't think we'd call such a language "Python" anymore though.* Exactly. Why shoehorn a language into an unintended purpose when you can just use another language with a different intended purpose? – Sam Estep Jul 05 '15 at 18:01
  • 5
    I didn't say 'Lets make a new language'. I simply asked a question which I don't think is that unambiguous or opinion based. Einther you can run Python statically or you can't. Yes or no question (-: Thank you for the answer @CrazyCasta. I learn't something from it. – Jonny Jul 05 '15 at 18:04
  • Oh, just to be technical, the grammar of your question would suggest you were asking about changing python. It sounds like what you're asking is "Does python have a statically typed mode" or "Are there ways of doing static typing in python". – CrazyCasta Jul 05 '15 at 18:05
  • 1
    "Does python have a statically typed mode" or "Are there ways of doing static typing in python" was indeed what I meant. I will definitely look into pypy! – Jonny Jul 05 '15 at 18:08
  • Python 3.6 supports type annotations and mypy (http://mypy-lang.org/) is a static type checker for Python. – Christophe Calvès Mar 22 '18 at 11:14
  • 1
    @ChristopheCalvès Yes and no. A. With or without a static type checker, python still isn't using this information. Unless I'm mistaken, the position of python is that type annotations is to support other projects, not to make python run faster or anything like that. B. Even if it were used for such, python is an expressive enough language that this would be a halting type problem. I.e. there would be valid code annotated a certain way that mypy would not be able to verify. Then the question becomes, how often would such a problem occur (i.e. practical or not). – CrazyCasta Apr 02 '18 at 03:33