Skip to main content

Home/ Mac Attack/ Group items tagged operators

Rss Feed Group items tagged

Benjamin Bandt-Horn

operators - What is the name of ** in python? - Programmers Stack Exchange - 0 views

  • It's not an operator as such, so it doesn't really have a name, but it is defined as a "syntactic rule". So it should be called: "the keyword argument unpacking syntax"
  • # usually a tuple, always an iterable*
  • # usually a dict, always a mapping*
  • ...4 more annotations...
  • kwargs
  • args =
  • *: Iterables are objects that implement the __iter__() method and mappings are objects that implement __iter__() and __getitem__()
  • If you are unsure what to call a particular operator or if it is unnamed, you can always resort to Waka Waka Bang Splat as a reference to help you figure out what to call it. In this case for ** I would call it double-splat, though there are some alternate names for symbols.
  •  
    It's not an operator as such, so it doesn't really have a name, but it is defined as a "syntactic rule". So it should be called: "the keyword argument unpacking syntax"
Benjamin Bandt-Horn

jcalderone: How to override comparison operators in Python - 0 views

  • here are the basic rules for the customization of ==, !=, <, >, <=, and >=: For all six of the above operators, if __cmp__ is defined on the left-hand argument, it is called with the right-hand argument. A result of -1 indicates the LHS is less than the RHS. A result of 0 indicates they are equal. A result of 1 indicates the LHS is greater than the RHS
  • __eq__ is not used for !=
  • For <, __lt__ is used. For >, __gt__. For <= and >=, __le__ and __ge__ respectively
  • ...1 more annotation...
  • __eq__ does an isinstance test on its argument
  •  
    here are the basic rules for the customization of ==, !=, , =: For all six of the above operators, if __cmp__ is defined on the left-hand argument, it is called with the right-hand argument. A result of -1 indicates the LHS is less than the RHS. A result of 0 indicates they are equal. A result of 1 indicates the LHS is greater than the RHS.
Benjamin Bandt-Horn

BitwiseOperators - Python Wiki - 0 views

  • x << yReturns x with the bits shifted to the left by y places (and new bits on the right-hand-side are zeros). This is the same as multiplying x by 2**y.
  • x >> yReturns x with the bits shifted to the right by y places. This is the same as //'ing x by 2**y.
  • x & yDoes a "bitwise and". Each bit of the output is 1 if the corresponding bit of x AND of y is 1, otherwise it's 0.
  • ...5 more annotations...
  • x ^ yDoes a "bitwise exclusive or". Each bit of the output is the same as the corresponding bit in x if that bit in y is 0, and it's the complement of the bit in x if that bit in y is 1.
  • x | yDoes a "bitwise or". Each bit of the output is 0 if the corresponding bit of x AND of y is 0, otherwise it's 1.
  • ~ x Returns the complement of x - the number you get by switching each 1 for a 0 and each 0 for a 1. This is the same as -x - 1.
  • Just remember about that infinite series of 1 bits in a negative number, and these should all make sense.
  • One more point: Python allows operator overloading, so some classes may be written to allow the bitwise operators, but with some other meaning. For instance, the new sets module for Python 2.3 uses | and & for union and intersection.
  •  
    The Operators: x << y Returns x with the bits shifted to the left by y places (and new bits on the right-hand-side are zeros). This is the same as multiplying x by 2**y.
Benjamin Bandt-Horn

Stackless Python - Wikipedia, the free encyclopedia - 0 views

  • Stackless Python, or Stackless, is a Python programming language interpreter, so named because it avoids depending on the C call stack for its own stack. The most prominent feature of Stackless is microthreads, which avoid much of the overhead associated with usual operating system threads. In addition to Python features, Stackless also adds support for coroutines, communication channels and task serialization.
  •  
    Stackless Python, or Stackless, is a Python programming language interpreter, so named because it avoids depending on the C call stack for its own stack. The most prominent feature of Stackless is microthreads, which avoid much of the overhead associated with usual operating system threads. In addition to Python features, Stackless also adds support for coroutines, communication channels and task serialization.
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

Why are there no ++ and --​ operators in Python? - Stack Overflow - 0 views

  • You don't write things like for(int i = 0; i &lt; 10; ++i) in Python very often; instead you do things like for i in range(0, 10).
  • it would add opcodes to the language (implying a larger, and therefore slower, VM engine)
  • in the "C" world it is most effectively used (not most commonly) with pointers. There is a direct mapping to some instructions sets that support pre- or post-increment of address registers
  • ...2 more annotations...
  • Python doesn't have tricks to convey intentions to the assembler because it doesn't use one.
  • this 'koan' also hints that increment/decrement operators are non-obvious
  •  
    You don't write things like for(int i = 0; i < 10; ++i) in Python very often; instead you do things like for i in range(0, 10).
Benjamin Bandt-Horn

Great Python Tricks for avoiding unnecessary loops in your code - Udacity Forums - 0 views

  • To do element-wise operations on a list - for example, to produce a list consisting of each element of list A multiplied by 2 Method 1 [2*x for x in A] - this technique is called list comprehension Method 2 if you have defined a function double(x) which doubles x map(double, A) Method 3 define the function that doubles x on the fly, anonymously map(lambda x: 2*x, A)
  •  
    To do element-wise operations on a list - for example, to produce a list consisting of each element of list A multiplied by 2 Method 1 [2*x for x in A] - this technique is called list comprehension Method 2 if you have defined a function double(x) which doubles x map(double, A) Method 3 define the function that doubles x on the fly, anonymously map(lambda x: 2*x, A)
Benjamin Bandt-Horn

Game programming - Wikipedia, the free encyclopedia - 0 views

  • The central component of any game, from a programming standpoint, is the game loop. The game loop allows the game to run smoothly regardless of a user's input or lack thereof. Most traditional software programs respond to user input and do nothing without it. For example, a word processor formats words and text as a user types. If the user doesn't type anything, the word processor does nothing. Some functions may take a long time to complete, but all are initiated by a user telling the program to do something. Games, on the other hand, must continue to operate regardless of a user's input. The game loop allows this. A highly simplified game loop, in pseudocode, might look something like this: while( user doesn't exit ) check for user input run AI move enemies resolve collisions draw graphics play sounds end while
  •  
    The central component of any game, from a programming standpoint, is the game loop. The game loop allows the game to run smoothly regardless of a user's input or lack thereof.
Benjamin Bandt-Horn

The Architecture of Open Source Applications: Python Packaging - 0 views

  • There are two schools of thought when it comes to installing applications
  • each application is its own standalone "appliance", and installing and removing them should not disturb the rest of the OS. If the application needs an uncommon library, that library is included in the application's distribution
  • a collection of small self-contained units called packages
  • ...8 more annotations...
  • Libraries are bundled into packages
  • Self-contained applications also make the developer's life easier when she needs to support several operating systems. Some projects go so far as to release portable applications that remove any interaction with the hosting system by working in a self-contained directory, even for log files.
  • Fellowship of the Packaging
  • In Python a package is a directory containing Python files. Python files are called modules. That definition makes the usage of the word "package" a bit vague since it is also used by many systems to refer to a release of a project.
  • use the term "Python packages" when we talk about a directory containing Python modules
  • The term "release" is used to define one version of a project
  • "distribution" defines a source or a binary distribution of a release as something like a tarball or zip file
  • advanced tools like Setuptools, which add features on the top of it, or Distribute, a fork of Setuptools. There's also Pip, a more advanced installer, that relies on Setuptools
  •  
    There are two schools of thought when it comes to installing applications
Benjamin Bandt-Horn

3. Data model - Python v2.6.6 documentation - 0 views

  • it is advised to somehow mix together (e.g. using exclusive or) the hash values for the components of the object that also play a part in comparison of objects.
  • If a class does not define a __cmp__() or __eq__() method it should not define a __hash__() operation either
  • Called when an attribute lookup has not found the attribute in the usual places (i.e. it is not an instance attribute nor is it found in the class tree for self). name is the attribute name. This method should return the (computed) attribute value or raise an AttributeError exception.
  • ...2 more annotations...
  • new-style classes
  • By default, instances of both old and new-style classes have a dictionary for attribute storage
  •  
    special method names
Benjamin Bandt-Horn

Elegant ways to support equivalence ("equality") in Python classes - Stack Overflow - 0 views

  • mixin class
  • Another issue with the __dict__ comparison is what if you have an attribute that you don't want to consider in your definition of equality (say for example a unique object id, or metadata like a time created stamp).
  • isinstance sucks
  • ...4 more annotations...
  • if type(other) is type(self):
  • Check types more strictly, like this:
  • is tests for object identity. This means a is b will be True in the case when a and b both hold the reference to the same object
  • __cmp__ was removed from python 3 so avoid it
  •  
    mixin class
1 - 11 of 11
Showing 20 items per page