[Edu-sig] re: Naming in Python

Kirby Urner urnerk at qwest.net
Sat Jan 29 17:18:05 CET 2005


Hi Art --

I've been thinking about your observation, but haven't come up with a really
coherent way to extend your thinking, in such a way that a prove I know what
you mean.  Per Wittgenstein (a philosopher I like, and his philo the focus
another eGroup I post in), if you go 1, 12, 42  and I go 92, 162, that sort
of proves I have an idea about the rule you're using (or maybe not?).

In Pythonic terms, you've written a generator, and I've reverse engineered
it, so now we're able to compare iterable.next() outputs, to see if we get
the same answers.

What I'm thinking sort of in parallel, divergently or convergently I don't
know yet, is that Python gets us away from any strict notion of
"primitives."  In other languages, lip service may be paid to "everything is
an object" but when you get right down to it, you need syntactical tricks to
make that a reality.

In C# you can "box" an integer, to treat it like a class object, and in Java
I guess you grab an instance of Integer or something.  But in neither
language can you do what you can in Python:  enter dir(2) in the shell and
get a dump of everything 1 "knows how to do" (something I show the first day
in my Python classes; I call it "making 2 spill its guts" (OK, these are
high schoolers, still somewhat in to grossology)).

As for decorators, at the moment I'm having no problems with the new syntax.
You may have seen that post on the calculus.  I was gratified to discover
that @ may be used to hand off the function that follows to a class (to some
__init__ method), so long as what's returned is likewise a function.  Wait,
is that what I did?  No, not exactly.  I fed the function to class and
thereby get back an *object*, but this object was *callable*, and that's
enough to make its behavior *like* a function's.  So everything worked.
Cool.

Here's the code again, which automatically lists 3rd degree polynomial
outputs *and* its 2nd degree derivative:

#====

class Function:
    def __init__(self, f):
	self.f = lambda x:  f(x)
    def __add__(self, other):
	return Function(lambda x: self.f(x) + other.f(x))
    def __sub__(self,other):
	return Function(lambda x: self.f(x) - other.f(x))
    def __mul__(self,other):
	return Function(lambda x: self.f(x) * other.f(x))
    def __div__(self,other):
	return Function(lambda x: self.f(x) / other.f(x))
    def __call__(self,x):
	"""call wrapped function with argument x, return result"""
	return self.f(x)

class Pair:
    def __init__(self, u, du):
	self.u  = u
	self.du = du
    def __add__(self, other):
	return Pair(self.u + other.u, self.du + other.du)
    def __sub__(self, other):
	return Pair(self.u - other.u, self.du - other.du)
    def __mul__(self, other):
	return Pair(self.u * other.u, \
                self.du * other.u + self.u * other.du)
    def __div__(self, other):
	return Pair(self.u/other.u, \
                (self.du * other.u - self.u * other.du)/other.u**2)
@Function
def ident(x):  return x

@Function
def one(x):  return 1

p1 = Pair(ident, one)

newp = p1 * p1 * p1

print [newp.u(i) for i in range(-5,6)]
print [newp.du(i) for i in range(-5,6)]

#====

Kirby




More information about the Edu-sig mailing list