By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,609 Members | 1,155 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,609 IT Pros & Developers. It's quick & easy.

C++ or Python

P: n/a
I would like to know what are advantages of Python in comparison with C
++? In which cases and why Python can be a better tool than C++?

Thank you!
Jun 27 '08 #1
Share this Question
Share on Google+
8 Replies

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Kurda Yon wrote:
I would like to know what are advantages of Python in comparison with C
++? In which cases and why Python can be a better tool than C++?

Thank you!
I'm a relative novice, so I couldn't tell you about deeply hidden
features or tricks, but there are a few advantages which influence my
choice to code in Python.

1) Portability. A well-written Python app is 100% system agnostic.
Sometimes, you just have to get dirty and make calls directly to the
system, but if you're creative, you can make do with pure Python calls.
This means that there's no porting of code. You don't have to maintain a
version for *nix's, a version for Windows XP, a version for Vista, and a
version for OS X. Although different versions for each allow for
fine-tuned optimization for individual systems, it's a hell of a lot of
work to take care of all that code.

A subset of this is that Python isn't compiled. With C++, even if your
code is completely system agnostic, you must compile a linux version, a
win32 (and nowadays, maybe even a 64) version, and a map version. With
each added compilation, you add the potential for bugs, breakage, and
other nastiness.

2) Python's coding style. This is a really broad category and great
subjective. I LOVE the whitespace coding. It makes code blocks really
look like blocks. Loops and if-elif-else blocks are clearly defined, and
it helps for maintenance, and it just makes my code look pretty.

Python also looks like English. My familiarity with the language has
some to do with it, but I can just READ the code. I don't have to write
down numbers and then look back to the code and try to interpret the
code. It reads like English.

3) I suppose it's sorta cheating to add Python's interpreted-language
status again, but it's worth reiterating, and an excellent example of
this will be illustrated by this story:

For one of my classes, we're allowed to do any kind of project we wanted
(assuming it was something school appropriate). I decided to code a
Dungeons and Dragons 3.5 Character stat generator in Python. On the day
my project was evaluated, I realized there was a bug in my code. Because
of some funky index slicing, numbers were being cut off. Thanks to
Python being interpreted rather than compiled, I was able to correct the
bug ON SITE, and present the properly functioning code.

4) Extensive libraries. Python has a vast, active community developing
libraries for just about anything you could ant. Pygame, Scipy,
wxPython, any kind of toolkit for any kind of job imaginable. The ease
of obtaining these tools helps me spend time on exercising my creativity
and coding skills, rather than spending time trying to find libraries
that do what I need.

Your mileage may vary, but for me, Python provides a reliable, portable
platform for things that I want or need. Yeah, for heavy-duty things
like gaming, heavy gaming, not tetris or Snake, C or C++ where memory
management is more available to the programmer is preferable. But for
everyday "I need to make a tool in 15 minutes" needs, Python's perfect.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkhli3oACgkQLMI5fndAv9gthgCfXUHwbwNQlR LjTwsdHiZuKqK2
bNAAoKe7+GYci3Pe9XloP3u+UOxLriTq
=bFi0
-----END PGP SIGNATURE-----
Jun 28 '08 #2

P: n/a
Kurda Yon <ku******@yahoo.comwrote:
I would like to know what are advantages of Python in comparison with C
++? In which cases and why Python can be a better tool than C++?
Python is a lot more fun than C++ ;-)

Anyway no need to use one or the other... I've done projects where
we've embedded python into into a large C++ program to give easy
scriptability to our application in a real language. I've also done
the reverse - embedded C/C++ into python. And for the really easy
option there is ctypes.

Given a blank canvas I'd start with python and then add a bit of C/C++
if some part of it was running too slowly. If there were existing
C/C++ libraries then I'd use ctypes to interface with them.

I don't think I ever want to start another large C++ app - been there,
done that, got the (mental) scars to prove it ;-)

All my humble opinion of course!

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jun 28 '08 #3

P: n/a


Kurda Yon wrote:
>
I would like to know what are advantages of Python in comparison with C
++? In which cases and why Python can be a better tool than C++?
Uh, code a trivial program in each language and you'll have a perfectly good
idea.

--
View this message in context: http://www.nabble.com/C%2B%2B-or-Pyt...p18176708.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Jun 29 '08 #4

P: n/a
On Fri, 27 Jun 2008 15:22:26 -0700, Kurda Yon wrote:
I would like to know what are advantages of Python in comparison with C
++? In which cases and why Python can be a better tool than C++?

Thank you!
Python's automatic tracebacks on errors facilitate debugging very nicely.

Python doesn't have much in the way of stray pointer references or out of
bounds array references (you'd pretty much have to use a poorly-written
extension module to get them), making development in and maintenance of
python code much less expensive in human time.

Python has garbage collection, which means you don't need to worry much
about memory leaks.

Python isn't just another curly brace language - it reads more like the
pseudocode you'd see scribbled on a white board, perhaps than any other
language.

Python's a very dynamic language - so there are things where C++ would
catch errors for you that Python wouldn't. However, this also means that
you don't spend time declaring variables over and over in Python. Also,
things like passing a method as a function parameter is a no-brainer
(requires extra syntax in java because of the cautious type system - not
sure about C++). Python's dynamicity also means that you don't need a
clunky mechanism like templates or have to use inheritance just to get
type parameters.

In python, everything is an object. Not everything has methods, but
everything can be stuck into a variable - modules, pieces of code,
whatever. Just try to stick an arbitrary header file's expansion into a
variable in C++.

Jun 29 '08 #5

P: n/a
Dan Stromberg <ds*************@gmail.com>:
things like passing a method as a function parameter is a no-brainer
(requires extra syntax in java because of the cautious type system - not
sure about C++).
C++ has function pointers and functors, therefore this is not really an
issue with C++.

--
Freedom is always the freedom of dissenters.
(Rosa Luxemburg)
Jun 29 '08 #6

P: n/a
On Sun, 29 Jun 2008 11:20:45 +0200, Sebastian \"lunar\" Wiesner wrote:
Dan Stromberg <ds*************@gmail.com>:
>things like passing a method as a function parameter is a no-brainer
(requires extra syntax in java because of the cautious type system -
not sure about C++).

C++ has function pointers and functors, therefore this is not really an
issue with C++.
Based on http://en.wikipedia.org/wiki/
Function_object#Functors_in_C_and_C.2B.2B it looks like there's no
special syntax in the language core, but programs needing to pass an
object as though it were a function (for example) do need to add some
extra lines of code for each object needing such treatment.

Jun 29 '08 #7

P: n/a
On Jun 28, 12:22*am, Kurda Yon <kurda...@yahoo.comwrote:
I would like to know what are advantages of Python in comparison with C
++? In which cases and why Python can be a better tool than C++?

Thank you!
Back in 2002, we made a hard decision between c++ (well established
here) java (not as mature back then) and python (completely new to my
company).
The task was to do a complete rewrite of our control software running
our fully automatic pallet conveyor systems.
The architecture is three-tier; host (not our system), service with
GUI (com uppwards and downwards plus supporting the operators of the
plant) and PLC nearest the el-mech.

We decided to go for python since we always could fallback on ansi c
in case of performance issues.

We have today no regretted this!
We have extended the architecure since with handhelds and they are
programmed with java due to lack of proper python possibilities (at
the time).
We have further programmed code generatord for the PLC code.
and so on.. :o)

Sure, c++ could have been used but not at the effort(lessness) we now
experience.

Mu 2 cents//Maria
Jul 1 '08 #8

P: n/a
2008/6/29 Dan Stromberg <ds*************@gmail.com>:
>
On Sun, 29 Jun 2008 11:20:45 +0200, Sebastian \"lunar\" Wiesner wrote:
Dan Stromberg <ds*************@gmail.com>:
things like passing a method as a function parameter is a no-brainer
(requires extra syntax in java because of the cautious type system -
not sure about C++).
C++ has function pointers and functors, therefore this is not really an
issue with C++.

Based on http://en.wikipedia.org/wiki/
Function_object#Functors_in_C_and_C.2B.2B it looks like there's no
special syntax in the language core, but programs needing to pass an
object as though it were a function (for example) do need to add some
extra lines of code for each object needing such treatment.
That's a slightly different case - creating a functor in C++ is more
equivalent to making a Python object callable. Both require you to add
a specially named method to the class - "__call__" or "operator()".

The syntax for the pointer to the member function Bar of class Foo is
"&Foo::Bar", which is the same as taking a pointer to anything else in
C++.

The standard C++ library also has functions which take a pointer to a
member function and return a functor which can be used to call the
member function on an instance. (Whether passing the pointer as an
unbound method works correctly with functions which expect a reference
to a functor is compiler dependent, so is discouraged in portable
code) So it's quite easy to pass an unbound method in C++ - the only
extra line of code is #include<functional>. The disadvantage is that
member function pointers mostly behave like functors, except when they
don't, so there's more cognitive load.

What is a lot easier in Python is to pass a bound method as a
first-class function - you just use "instance.method_name" - but in
C++ there isn't a standard library function that takes an object
reference and a member function pointer and returns a functor bound to
that member. In the absence of automatic memory management, you have
to ensure the instance lifecycle exceeds the functor lifecycle, and
the standard library avoids making design decisions about lifecycles
of objects it doesn't create.
Jul 2 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.