Skip to main content

Home/ Mac Attack/ Group items tagged conditional

Rss Feed Group items tagged

Benjamin Bandt-Horn

Ternary conditional operator in Python - Stack Overflow - 0 views

  •  
    Yes, it was added in version 2.5. It's frowned upon by some pythonistas, so keep that in mind. The syntax is: a if test else b First test is evaluated, then either a or b is returned based on the Boolean value of test; if test evaluates to True a is returned, else b is returned. For example: >>> 'true' if True else 'false' 'true' >>> 'true' if False else 'false' 'false'
Benjamin Bandt-Horn

Classes & Iterators - Dive Into Python 3 - 0 views

  • Comprehensions are just a simple form of iterators. Generators are just a simple form of iterators
  • The first argument of every class method, including the __init__() method, is always a reference to the current instance of the class. By convention, this argument is named self.
  • self is not a reserved word in Python, merely a naming convention
  • ...6 more annotations...
  • To build an iterator from scratch, Fib needs to be a class, not a function.
  • in most cases), __iter__() simply returns self, since this class implements its own __next__() method.
  • a for loop will call this automatically, but you can also call it yourself manually
  • the __next__() method raises a StopIteration exception, this signals to the caller that the iteration is exhausted. Unlike most exceptions, this is not an error; it’s a normal condition that just means that the iterator has no more values to generate. If the caller is a for loop, it will notice this StopIteration exception and gracefully exit the loop. (In other words, it will swallow the exception.)
  • Do not use yield here; that’s a bit of syntactic sugar that only applies when you’re using generators. Here you’re creating your own iterator from scratch; use return instead
  • raise StopIteration
  •  
    Comprehensions are just a simple form of iterators. Generators are just a simple form of iterators
1 - 2 of 2
Showing 20 items per page