• Hi,
    You’ll find that Python does have a ternary operator, it’s just different in syntax.
    You can do:

    if you haven’t come across list comprehensions yet then you have more goodies in store.

    – Paddy.

  • logan

    Hi, regarding the ternary operator you can also “short-circuit” boolean evaluation

    pretty cool eh?

  • Tudor, Logan;
    Now that Python has its own ternary operator we should use it. Before the ternary operator we /had/ to rely on short-circuit evaluation, now we should let it recede to its remaining uses.

    – Paddy.

  • logan> cool trick, but it only works when b doesn’t evaluate to false.

    if I were to write the same snippet in php using the ternary operator, it would look like:

  • Paddy> what do you mean by now? Python 3.x? In my console things look like this:

    If they’ve added the ternary operator to the new version of the language, this is great news.

    But I think the lack of the pre/post increment operator is even more annoying, as it doesn’t throw any errors. It just doesn’t work. Yes, I know why it doesn’t throw any errors, but I still find annoying :p

  • Regarding the post/pre increment, I got the impression those are not present because they’re too susceptible to misuse which results in bugs. Many of the features of Python seem to be designed intentionally to prevent people from making common mistakes, like the lack of an ‘unless’ statement which I’ve seen lead people to more boolean logic errors in Perl than I care for.

    For your class, you should have it inherit from object, otherwise you’re using old-style classes, and not the new-style classes from Python 2.2 and on. I.e.:

    As Paddy mentioned, the syntax for ternary is different in Python, it was added in Python 2.5:

  • The syntax and keywords used in the Python ternary operator are not those of C. you would write:

    Pythons designers looked at common cases of errors in C programming and found that ++ and — can be very confusing. Their is nothing stopping people writing C code with ++ and — that can be both implementation dependant in its output and confusing to read. for example, what does this mean?

    for similar reasons, Python does not allow assignment as an expression – this means that you cannot assign to a name in an if statements condition.

    It is all part of Pythons wish to be easy to read and so maintain. Some of the ommisions might be irksome at first, but when you read other peoples code, it should pay off.

    – Paddy.

  • I remember that when I have started learning how to use python classes I have found a way to make attributes someway private. They weren’t private in the sense that Java, C++ or C# handles them, but it was a little hack that wouldn’t allow you to handle their values directly.
    When I’ll have the time I’ll dig in and show you how to do it if you don’t discover it by then.

  • It’s fairly easy to obtain getters/setters behind attributes in python, which can then handle access to them. One would just have to use property()

    In my opinion access modifiers don’t mean a language has better OOP capabilities. PHP5 introduced these, but were there any advantages? I believe not. On the other hand, magic methods like __get/__set and __call really improved the language.

    It’s the same with python. You don’t get access modifiers, but you get tons of other things that are impossible in PHP or Java.

    The only thing I don’t like about python is its lambda form. Anyway, lambdas should not be abused as it makes for a harder to test/extend API.

    Oh, and the strange join() method of strings. I’m used to it being on sequences rather than strings.

Advertisment ad adsense adlogger