Skip to main content

Home/ Mac Attack/ Group items tagged Python

Rss Feed Group items tagged

Benjamin Bandt-Horn

Python : map and filter vs for loops - 0 views

  • Re: Python : map and filter vs for loops The underlying implementation of map and filter are different than loops, even though they accomplish the same thing. Which idiom runs fastest can change from version to version. Right now I think list comprehensions are the fastest. So instead of Code: filter(f, range(2,25)), the list comprehension would be Code: [x for x in range(2,25) if f(x)] and instead of Code: map(cube, range(1, 11)) the list comprehension would be Code: [cube(x) for x in range(1,11)]
  •  
    Python : map and filter vs for loops The underlying implementation of map and filter are different than loops, even though they accomplish the same thing. Which idiom runs fastest can change from version to version. Right now I think list comprehensions are the fastest.
Benjamin Bandt-Horn

''.format_map() in Python 2.x - 0 views

  • """''.format_map() in Python 2.x"""
  •  
    ''.format_map() in Python 2.x
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

Python 3D Software - 0 views

  •  
    A small collection (94 packages) of pointers to Python software for working in three dimensions
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

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

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

Simple Pythonic Rendering Engine - 0 views

  • Simple Pythonic Rendering Engine.
  •  
    Module and Class Hierarchies
Benjamin Bandt-Horn

gameobjects - Algorithms for games and realtime applications - Google Project Hosting - 0 views

  •  
    Game Objects is a collection of 2D and 3D maths classes, and algorithms for helping in the creation of games with Python. Suitable for PyGame, but independent of it.
Benjamin Bandt-Horn

Panda3D - Free 3D Game Engine - 0 views

  •  
    Panda3D is a game engine, a framework for 3D rendering and game development for Python and C++ programs.
Benjamin Bandt-Horn

The Ren'Py Visual Novel Engine - 0 views

  •  
    Ren'Py Visual Novel Engine is a free software engine which facilitates the creation of visual novels, a form of computer-mediated storytelling. Ren'Py is a portmanteau of ren'ai, a type of game made using Ren'Py; and Python.
Benjamin Bandt-Horn

4. More Control Flow Tools - Python v2.7.6 documentation - 0 views

  • *name must occur before **name.
  • keys = sorted(keywords.keys()) for kw in keys: print kw, ":", keywords[kw]
  • Note that the list of keyword argument names is created by sorting the result of the keywords dictionary’s keys() method before printing its contents; if this is not done, the order in which the arguments are printed is undefined
  • ...11 more annotations...
  • They are syntactically restricted to a single expression
  • Like nested function definitions, lambda functions can reference variables from the containing scope
  • Coding Style
  • CamelCase for classes
  • lower_case_with_underscores for functions and methods
  • Always use self as the name for the first method argument
  • comments
  • docstrings
  • separate
  • 79 characters
  • 4-space indentation, and no tabs
  •  
    keys = sorted(keywords.keys()) for kw in keys: print kw, ":", keywords[kw]
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

Practical guidelines for beautiful Python code | TurnKey Linux Blog - 0 views

  • a well defined mental model of the problem domain
  • Refining the architecture is part of the "refactor mercilessly" rule
  • If you've never inherited from a built-in data type, experiment with a small test case in a throw away script. This way you don't mangle your current project and you can be as experimental as you like.
  • ...3 more annotations...
  • constants are always class level attributes because they are shared by all instances
  • code is communication
  • If a method doesn't need access to instance level attributes then it should be a class method, not a regular method. If a method doesn't need access to class level attributes then it should be a static method, not a class method.
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

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

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()
‹ Previous 21 - 40 of 60 Next ›
Showing 20 items per page