Ad
  • Default User Avatar

    I agree with you, it was just a trick to lose some code. A lot of the solutions here are not production standard, often more pared down or employing tricks.

  • Custom User Avatar

    I'm personally not a fan of this solution, since you are not storing the first and last name seperately in the object. This might have a use later.
    The formatting into a string should happen in the get_full_name method.

  • Custom User Avatar

    I guess the comments make it clear. @Blind4Basics, when I first read your comment and then I read the comprehension it does make sense. So I guess I can say that it would be best practice to write such a thing when it has comments so everyone knows what's happening.

  • Custom User Avatar

    Note that "code that is clear to beginner" does not mean "code that looks pretty much like imperative, C code".

    This is a very common beginner mistake by itself, so saying that sentence would be quite hypocritical ;-)

  • Default User Avatar

    I would never write this code in a project with other members

    When you'll be used to python, you'll change your mind about that... ;) List or generator comprehensions are, when not too much convoluted and with meaningful variable names, easier to read because you read those like you would say your algo out loud:

    "get the name for all the names in the list of personns if the name contains 5 letters at least"
    
    => filtered = [name for name in lstName if len(name) >= 5]
    

    And this is SO MUCH simpler/shorter than:

    filtered = []
    for name in lstName:
      if len(name) >= 5:
        filtered.append(name)
    
  • Custom User Avatar

    Alright, thanks for your answer!
    It really makes it much clearer to me why this way of programming would be used. It is very true that a programming language needs to evolve and create easier ways to programs certain things.

    I do however still stand by my point, and this is my own opinion, that this is too unclear and I would never write this code in a project with other members.

    When code is written this way, I really believe it should have some comments to explain what it does.

    PS: I never said this was wrong ;)

  • Custom User Avatar

    The reason it is best practice, is because it is idiomatic Python code. It's neat, self contained, and self explanitory. And most importantly comes naturally with familiarity of the language.

    As a beginner, you may be unfamilar with comprehensions, in which case the solution may initially look a little alien, I had that difficulty when I first took up the Python, especially with the "if" coming after the "for loop".

    However what you are sugesting - that all code be written so beginners are able to read it, would mean that new constructs could never become common usage, and the language syntax would be complete upon first release.

    I'd argue that there is some middle ground, beginners attempt to learn what they don't understand, and the more experienced don't write incomprehensibly complex code - just because they can.

    In this case however, I'd argue that 1000+ identical correct submissions can't all be wrong. :)

  • Custom User Avatar

    Al lot of people are calling this best practice, but I don't really get that. It's a very smart and clever solution, but it is not clear at all. That's a requirement for being best practice! Good code is readable to beginners, even when written by an expert.
    Correct me if you believe I'm wrong, I'd like to hear your opinions about this.