Skip to main content

Home/ Mac Attack/ Group items tagged stack

Rss Feed Group items tagged

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

Stack data structure in python - Stack Overflow - 0 views

  • No need to jump through these loops, See 5.1.1 Using Lists as Stacks
Benjamin Bandt-Horn

How does polymorphism work in Python? - Stack Overflow - 0 views

  • idiomatic Python dictates that you (almost) never do type-checking, but instead rely on duck-typing for polymorphic behavior. There's nothing wrong with using isinstance to understand inheritance, but it should generally be avoided in "production" code
  •  
    idiomatic Python dictates that you (almost) never do type-checking, but instead rely on duck-typing for polymorphic behavior. There's nothing wrong with using isinstance to understand inheritance, but it should generally be avoided in "production" code
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

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

python - *args and **kwargs? - Stack Overflow - 0 views

  •  
    One case where *args and **kwargs are useful is when writing wrapper functions (such as decorators) that need to be able accept arbitrary arguments to pass through to the function being wrapped. For example, a simple decorator that prints the arguments and return value of the function being wrapped: def mydecorator( f ):   @functools.wraps( f )      def wrapper( *args, **kwargs ): ...
Benjamin Bandt-Horn

The new print function in Python 3.x - Stack Overflow - 0 views

  • If print() is a function, it would be much easier to replace it within one module (just def print(*args):...) or even throughout a program (e.g. by putting a different function in __builtin__.print). As it is, one can do this by writing a class with a write() method and assigning that to sys.stdout -- that's not bad, but definitely a much larger conceptual leap, and it works at a different level than print.
Benjamin Bandt-Horn

!performance - Python string formatting: % vs. .format - Stack Overflow - 0 views

  • which is just ugly. .format doesn't have those issues. Also in the second example you gave, the .format example is much cleaner looking.
  •  
    .format just seems more sophisticated in many ways. You can do stuff like re-use arguments, which you can't do with %. An annoying thing about % is also how it can either take a variable or a tuple
Benjamin Bandt-Horn

Python: How do I pass a variable by reference? - Stack Overflow - 0 views

  • Parameters are passed by value
  • some data types are mutable, but others aren't
  • If you pass a mutable object into a method, the method gets a reference to that same object and you can mutate it to your heart's delight, but if you rebind the reference in the method, the outer scope will know nothing about it, and after you're done, the outer reference will still point at the original object.
  • ...5 more annotations...
  • List - a mutable type
  • String - an immutable type
  • you could return the new value. This doesn't change the way things are passed in, but does let you get the information you want back out:
  • use_a_wrapper_to_simulate_pass_by_reference
  • But sometimes the thing was a pointer
  •  
    Parameters are passed by value
Benjamin Bandt-Horn

Binary Tree in Python - Stack Overflow - 0 views

  • def PreOrder(self): print self.data if self.left: print self.left.PreOrder() if self.right: print self.right.PreOrder()
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 < 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

python - if A vs if A is not None: - Stack Overflow - 0 views

  •  
    The statement if A: will call A.__nonzero__()
Benjamin Bandt-Horn

python - Difference between "if x" and "if x is not None" - Stack Overflow - 0 views

  •  
    In the following cases: test = False test = "" test = 0 test = 0.0 test = [] test = () test = {} test = set() the if test will differ: if test:#False if test is not None:#True
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
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

How to implement __iter__(self) for a container object (Python) - Stack Overflow - 0 views

  • usually __iter__() just return self if you have already define the next() method (generator object)
  • While you are looking at the collections module, consider inheriting from Sequence, Mapping or another abstract base class if that is more appropriate. Here is an example for a Sequence subclass:
  • if hasattr(self.data[0], "__iter__": return self.data[0].__iter__() return self.data.__iter__()
  •  
    if not self.data: raise StopIteration
Benjamin Bandt-Horn

object - Build a Basic Python Iterator - Stack Overflow - 0 views

  • I just found xrange() (suprised I hadn't seen it before...) and added it to the above example. xrange() is an iterable version of range() which has the advantage of not prebuilding the list
  •  
    I just found xrange() (suprised I hadn't seen it before...) and added it to the above example. xrange() is an iterable version of range() which has the advantage of not prebuilding the list
1 - 17 of 17
Showing 20 items per page