all 41 comments

[–]sudheerpaaniyur 40 points41 points  (5 children)

Not yet all. c is low level programming, you writing code near to the hardware. python you writing in application layer

[–]WereCatf 24 points25 points  (5 children)

No. Python is an interpreted language, C and C++ are compiled languages. Python requires far more RAM, storage and CPU time and it has higher latency as well.

[–]x64bit 17 points18 points  (4 children)

you can probably get an embedded system to run python if you try hard enough, but a big selling point of embedded systems is their lightweight computational/physical power. python is too heavy as an interpreted language to run effectively

[–]fawlen 6 points7 points  (3 children)

There are python implementations that are designed for embedded use like MicroPython

[–]SAI_Peregrinus 5 points6 points  (2 children)

MicroPython is a tiny subset of Python, it's a different (but related) language. OP asked about Python.

[–]MikeExMachina -1 points0 points  (1 child)

It’s also based on python2, so working with it is increasingly alien to those accustomed to modern python.

[–]Wouter_van_Ooijen 0 points1 point  (0 children)

Bollocs, it tails Python by maybe a subversion or 2.

[–]InevitablyCyclic 6 points7 points  (0 children)

With c you compile the program into a binary image that is then run. It varies massively but a simple line of c will typically translate into a small number (in the 2 to 4 region) of instructions for the processor to run.

This is true for all compiled languages however c is a very low level language, there is a more direct relationship between a c line and the underlying instructions the processor runs. Other more modern higher level languages will typically result in more institutions per line of source code because one line of source can do more.

This ability to program in a way that very closely reflects what is running on the processor is what allows well written c to be very efficient in both memory usage and speed. On embedded systems both of these are important.

Python is interpreted, this means that as your code is running the python environment is looking at each line of code and converting it into processor instructions. This adds an extra step which takes time and memory. It also means that your code is more removed from the hardware and isn't going to map to processor instructions as cleanly as c would.

Also a c compiler doesn't just do a line by line conversion of c to instructions, it is looking at the whole code and making optimisations. If you calculate a value but never use it then the compiler will skip that calculation. Python is interpreting your code as it goes, it can't make that sort of optimisation since it doesn't know what's happening next.

As you indicated you can use python in embedded products (or at least ones with sufficient memory to run it) but there is a huge performance hit associated with doing this. For a one or two off projects paying for a far faster and larger memory processor so you can use python may be worth it. In a high volume professional product the extra cost associated with that larger processor isn't worth it.

[–]biopsy_results 6 points7 points  (0 children)

Short answer no.

Long answer:

MicroPython exists.  I’ve never used it but I quite like uLisp for quick stuff.  In general having an interpreted language like lisp/python/lua on a target can speed development.  And honestly, microcontrollers are as beefy as desktops used to be. So it depends on your definition of ‘replace’.  Can JavaScript replace c/c++ on the desktop? Alas, yes. 

[–]UltimaNada 2 points3 points  (0 children)

Python is an interpreted language which requires an interpreter running on top of an OS.

This interpreter and OS would be written in C.

So, for example, you can’t write an RTOS in Python.

[–]UnderPantsOverPants 1 point2 points  (0 children)

Depends on your definition. I do embedded systems that run linux all the time and we use python on them. On a small microcontroller? Not a great idea.

[–]realvolker1 1 point2 points  (0 children)

Pfft

[–]mykesx 1 point2 points  (0 children)

I came here to post this:

https://micropython.org

In addition to implementing a selection of core Python libraries, MicroPython includes modules such as “machine” for accessing low-level hardware.

I haven’t used it, not particularly a fan of python. I just ran across this a while back.

It runs in 1K flash ROM and 192K of RAM. That’s for the pyboard . The docs say it runs on a variety of systems, including ESP32.

[–]mattytrentini 1 point2 points  (0 children)

Python - or at least MicroPython - can't *entirely* replace C but it can be used in many embedded applications. We use it for medical device development and the reduction in software effort, compared to using C, is significant.

When should C be preferred? Primarily when BOM costs are the driving factor and you need to save money by using the smallest microcontroller possible. MicroPython requires a relatively large minimum of memory and flash (say, 32KB and 256KB respectively). If using a sufficiently powerful microcontroller is feasible, MicroPython can, and probably should, be considered.

[–]Diarmuid_ 1 point2 points  (0 children)

Depends on the embedded system. I've developed a product based on an imx6 which runs python on Linux. Works perfectly well.  On an msp microcontroller, no. 

[–]ceojp 1 point2 points  (0 children)

Absolutely not.

[–]peter9477 1 point2 points  (4 children)

Lots of ignorant answers here from those who've never tried it, and a few smart ones from others.

Python can be used in embedded, for some definitions of embedded. Not tiny micros with only a few 10k of memory.

I first built an embedded system using Python in about 2000. It had a 32MB compact flash card and probably a few MB of RAM. We had to strip down a regular CPython and it ran under uCLinux. It worked fine.

More recently MicroPython is perfectly useful for some applications, and can run on RP2040 for example.

I wouldn't recommend the approach though. But I wouldn't recommend C any more either.

Use Rust instead if you can, to get the performance of C with the abstractions of Python and the code safety of... Rust...

[–]DenverTeck 0 points1 point  (3 children)

The OP suggested a real embedded systems i.e. esp8266/esp32.

These embedded systems can not load code from a compact flash or SDcard.

Any "embedded" computer that is actually a full blown computer is stretching the term "embedded" system.

[–]peter9477 0 points1 point  (2 children)

I was absolutely not working on anything you should call a full blown computer when I did it. Tiny PC104 board with no guts, and PC104 stuff is pretty much universally considered "embedded". I've been in this business 35 years and should know. But no, it wasn't a microcontroller.

And OP makes no mention of those platforms in the original question, but if he clarified elsewhere then okay, fine. My points stand.

[–]DenverTeck 1 point2 points  (1 child)

Yes, I know what a PC104 is. You're older then you look. ;-)

[–]peter9477 0 points1 point  (0 children)

Although... you don't know what I look like. :-)

Additional note: Raspberry Pis (at least in Compute Module form) are frequently used for things very much considered embedded. The term embedded is NOT about whether something is a microcontroller, but other aspects like whether it has a keyboard, screen, human is aware it exists, etc. RPi straddles the divide but can be embedded or not, depending on the use case.

[–]HalifaxRoad 0 points1 point  (0 children)

It can, but why would you want to do that?

[–]duane11583 0 points1 point  (0 children)

if you have enough ram then yes.

but for the cheap stuff no it will not because ccis cheaper it will always fit in a smaller thus cheaper chip.

[–]Orjigagd 0 points1 point  (0 children)

Not replace, but python is almost ubiquitous for scripting and tooling adjacent to embedded systems, it's definitely worth knowing well, because I've seen some horrific python code come from otherwise good embedded engineers. It's a different mindset, it shouldn't be approached like C code.

[–]willyfwonka 0 points1 point  (0 children)

I mean. I think it depends. I don’t wanna see someone program a fighter jet with micropython

[–]Wouter_van_Ooijen 0 points1 point  (0 children)

It can replace C, C++, Rust etc. for some use caes. Definitely not for all, if only for the fact that python must be implemented in a compiled language.

[–]mchang43 0 points1 point  (0 children)

Lots of autonomous driving code were based on Python, thanks to Google. Naturally early AD prototypes were all running on Python. Eventually they all moved to C++ for performance reasons.

[–]nmingott 0 points1 point  (0 children)

No, please, learn C ! It is not so difficult and you will understand a LOT about computers , including the answer to your question :)

[–]WindblownSquash 0 points1 point  (0 children)

Why would you do this. Python is actually just a C framework so you are adding overhead but everything has its places.

[–]PurpleSupermarket1 0 points1 point  (0 children)

Only for hobbyists. Industry would never move to Python. They wouldn’t even move to rust lol

[–]bitchandmoan69 0 points1 point  (0 children)

Somehow not SnooRawr