Skip to main content

Home/ Mac Attack/ Group items tagged overloading

Rss Feed Group items tagged

Benjamin Bandt-Horn

Function overloading in Python: Missing - Stack Overflow - 0 views

  • keyword arguments with default values can go a long way.
  • In Python, I think it's more accepted to use duck typing -- asking what an object can do, rather than what it is
  • it goes against the spirit of Python to worry a lot about what types are passed into methods
  •  
    keyword arguments with default values can go a long way.
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.
1 - 2 of 2
Showing 20 items per page