Subtleties between Python and C

Asked

Viewed 102 times

0

Good night! Sunday I discovered that I must learn Python, that the subject I will attend will be in that language and there is no way out. I studied well this week and managed to get to matrices, but I came across subtleties that I am still trying to understand. One of the exercises I did today asked me to do a function that returned if a given object belonged to a list or not, I did "two" functions that follow:

def Pertence1 (item, lista):
    return item in lista

def Pertence2 (item, lista):
    for i in range(len(lista)):
        if (item == lista[i]): return True
    return False

This second function was the first one I thought, as I have as language "mother" C, (if I don’t do a function that traverses a list (vector), I even have an itch), came almost automatic imagine a loop that traverses a list and compares item by item until I find, in a second moment came the first function, which I feel is not exactly a "function" (I am still understanding this concept in python), simpler, objective, fulfilled what the exercise asked, but I found in a way, very simple and even obscure, thinking of a huge code I understand how much more useful the first one would be. I do not know if with C I have learned that a good code, consistent, needs to be robust, hard, with kkk pointers. By analogy between English and Portuguese, I feel that if the requirement to learn another language was just vocabulary, it would be enough to memorize the words, but that’s not the case, there are epistemological subtleties between describing the same object in two languages, even if the logic is the same to describe. I’d like your opinion on.

  • 1

    Nor will I use the answer space because, as you say, at the end of the question, the answers will be based on opinion and I’d rather not risk being outside the scope of the site, but my opinion is that, contrary to what you think, the first one is much more elegant and, most important, most pythonica.

  • 1

    And yes, this is not an opinion, in the first function the list is, tb, "traversed" to find the element (pq go through all?). If you think the first one is inelegant pq is very simple, I suggest you open a Pyton terminal and type: import this <enter>, read very carefully and fall in love with python. :-)

  • 2

    I believe that understanding if, for, while, basic functions and variables you will already have the basics, comparing both languages you will not get anywhere, what you need to learn (as much as you believe you already know) is programming logic and the python language, compare with C will only get you more upset.

  • @Sidon I think I expressed myself wrong to classify as inelegant, deep down I wanted to say that it is obscure, like, what is the execution time of this call? Does it behave the same way with ordered lists? I still do not know the functioning of these intrinsic "functions", I will follow your advice and read what you proposed.

  • @Guilhermenascimento As my only reference in programming is C, I think it was kind of inevitable not to compare, so I tried to raise this question already to avoid thinking that C is better than Python and vice versa, I think that this question doesn’t even make sense. was a shock to find this persistence call where I don’t need to write a code snippet that does something exhaustively.

  • Gostaria da opinião de vocês sobre. No. We are morally obligated to close every question that asks opinions.

Show 1 more comment

1 answer

4


What you describe is what we call idiomatic code.

What is "idiomatic expression" in programming?

Obviously there will be various ways of creating a function that returns the desired result, but not all of them will be idiomatic; that is, not all of them will apply to the philosophy that language preaches. In C, it makes sense that you have to go through the list until you find the desired value; it was designed to be so and it works very well. Python, in turn, preaches that the code should read the most readable to humans; you write your program almost as if writing a text. There is no way to compare solutions in different languages, let alone choose which one is better. Still, in Python, the idiomatic solutions we call pythonic.

What is a "pythonic code"?

Now, answering your question, of course, in Python, the first solution makes much more sense than the second. Simple is Better than Complex. If there’s a simple way, why complicate it? And I still say that if it wasn’t like an exercise proposal, it wouldn’t even make sense for you to define a function to execute that logic. It would be much simpler and readable if you use the condition directly value in list than need to call a function for that. Readability Counts.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.