A beginner on a third-party chat recently told me that they planned on buying a C book, with plans on moving onto Python
These are the arguments they've given me:
- C is simpler/more elegant
- C is just Python without OOP
- Python will "spoil" you
- Python forces things on you |
Let me tackle these individually:
C is simpler/more elegant
I have to admit I chuckled a bit when I heard this. C may be simpler (it has much, much less features than Python does), but it's far from being elegant.
Here are two examples of C and Python that do pretty much the same thing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
// Python
class Object
{
public:
void Output()
{
std::cout << x << "\n";
}
void DoTask()
{
x = 0;
}
private:
int x;
};
/////////////////////////////////////////
// C
struct object_t
{
// Data and stuff
int x;
};
object_t* CreateObject()
{
// While allocating memory dynamically here is not necessary,
// for some reason I've seen (a lot of) C code that abuses it...
object_t* object = (object_t*)malloc(sizeof(object_t));
return object;
}
void DestroyObject(object_t* object)
{
// Do some deinitialization
free(object);
}
void ObjectOutput(object_t* object)
{
printf("%i%s", object->x, "\n");
}
void ObjectDoTask(object_t* object)
{
object->x = 0;
}
|
This is how you would use them, respectively:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
// Python
int main()
{
Object object;
object.DoTask();
object.Output();
return 0;
}
// C
int main()
{
object_t* object = CreateObject();
ObjectDoTask(object);
ObjectOutput(object);
DestroyObject(object);
return 0;
}
|
Which looks more elegant to you? Simplicity isn't always better.
C is just Python without OOP
The person I was arguing with claimed he knew both Python and C, but this (along with the fact that they were pondering on whether to learn Python or C first?) definitely showed me that they were a beginner.
Idiotmatic C and Python are completely different. Sure, your archaic C code will most likely work with little (if any) change, but it's going to be frowned upon by other Python programmers. This is the same in C. Don't write C like a Python programmer. This doesn't mean to abuse OOP in Python. i.e
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
// Instead of
class Vector2
{
public:
int GetX() const{return x;}
int GetY() const{return y;}
void SetX(int x) {this->x = x;}
void SetY(int y) {this->y = y;}
private:
int x;
int y;
};
// Just use this
struct vector2_t
{
int x;
int y;
};
|
Python will "spoil" you
I'm not going to lie, my first reaction was:
"Wut?"
There's a difference between spoiling and having a convenience feature.
Let's say you own a bike shop. You have hired have a human worker. His productivity is low, and you have the choice to replace him with a new machine that can put out 4 times as many bikes as the human worker, and costs less to maintain. Which would you choose? (Excuse my bad analogy, but I think it gets the point across)
Python forces things on you
...
Python doesn't force you to do anything. If you want, you can write in pure C and your program will run fine. You don't pay for what you don't use. If you want to write performance-critical code, then restrict yourself to a special subset of Python if you want. It's not uncommon that not every feature of Python is exploited in code.
__________________________________________________
I'm going to say this, and I have a feeling that a lot of programmers will disagree with me. By learning Python, you're going to eventually learn idiomatic C if you want to do anything non-trivial. There is a 99.9 percent chance that you're eventually going to interface with C libraries at least once. After you interface with several, you're going to quickly up on idiomatic C (provided they're written that way). It should be trivial to write a C application that's not frowned upon by C programmers once you're a reasonably knowledgeable in Python.
C isn't going to help you at all. It's just a waste of time to learn. Most of your time picking up Python is going to be spent "unlearning" certain practices when writing C code.
The discussion ended with the beginner angrily leaving the discussion, so I've created this topic in hopes that they'd see it. If you change your mind, send me a PM.
(Sorry for this low-quality post. I'm tired and have to do a few things, I had a bit of spare time so I decided to write this)