The grokking eagle

Hoc age

Posts Tagged ‘python’

classmethod python

Posted by arnulfo on 2007/07/26

classmethod(function)

Returns a class method for function.

A class method receives the class as implicit first argument, just like an instance method receives the instance. To declare a class method, use this idiom:

class C:
    @classmethod
    def f(cls, arg1, arg2, ...): ...

The @classmethod form is a function decorator — see def for details.

It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class. If a class method is called for a derived class, the derived class object is passed as the implied first argument.

My personal experience is that I almost *never* want a staticmethod.
The things that I would have written as a staticmethod in Java I simply
write as a module-level function in Python.

I do occasionally used classmethods though to build alternate
constructors. I recently had a class with a constructor that looked like:

class C(object):
def __init__(self, *args):

I already had code working with this constructor, but I needed to add an
optional ‘index’ parameter. I found the clearest way to do this was
something like:

class C(object):
def __init__(self, *args):

@classmethod
def indexed(cls, index, *args):

obj = cls(*args)
obj.index = index

return obj

Then the classes that needed the ‘index’ parameter simply use
C.indexed() as the constructor instead of C().

STeVe

Posted in Uncategorized | Tagged: , | Leave a Comment »

Python Generator Tricks

Posted by arnulfo on 2007/07/26

By Pramode C.E.

The Python programming language’s support for generators is described in PEP 255. This article demonstrates a few simple programs which make use of this feature to do some fun stuff like filtering out prime numbers, representing an `infinite’ series expansion in a finite way, applying the Euler `accelerator’ to make a series converge faster etc. Many of the programs which I describe here have been taken from `test_generators.py’ which is available with the Python source distribution. A few ideas have been stolen from the Computer Science classic, Structure and Interpretation of Computer Programs.

Read the rest of this entry »

Posted in Uncategorized | Tagged: , | Leave a Comment »

and or python

Posted by arnulfo on 2007/07/26

4.6. The Peculiar Nature of and and or

 

In Python, and and or perform boolean logic as you would expect, but they do not return boolean values; instead, they return one of the actual values they are comparing.

This trick may seem like more trouble than it’s worth. You could, after all, accomplish the same thing with an if statement, so why go through all this fuss? Well, in many cases, you are choosing between two constant values, so you can use the simpler syntax and not worry, because you know that the a value will always be true. And even if you need to use the more complicated safe form, there are good reasons to do so. For example, there are some cases in Python where if statements are not allowed, such as in lambda functions.

 

Further Reading on the and-or Trick

Posted in Uncategorized | Tagged: , , , | Leave a Comment »

SAGE

Posted by arnulfo on 2007/07/03

SAGE is a different approach to mathematics software.

Read the rest of this entry »

Posted in Uncategorized | Tagged: , , , , , | Leave a Comment »