Re: [Tutor] church numerals (fwd)

2006-11-12 Thread Danny Yoo
[Forwarding to tutor.

Asrarahmed, please learn to use the "Reply to All" feature on your email 
client.]


-- Forwarded message --
Date: Sun, 12 Nov 2006 11:03:42 +
From: Asrarahmed Kadri <[EMAIL PROTECTED]>
To: Danny Yoo <[EMAIL PROTECTED]>
Subject: Re: [Tutor] church numerals

> Believe it or not, these things actually work like numbers:
>
> #
 def iszero(n):
> ... def s(x):
> ... return False
> ... return n(s, True)


Where is n() defined  ...?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] dump class question

2006-11-12 Thread johnf
Hi,

Is it possible to a single that contains two classes:

Myclass.py file contains:

Class one(object):
  def needsomething(self):
Class two (object):
   def dosomething(self):

I want Class one's methods to access Class two methods?

Class one(object):
  def needsomething(self):
return dosomething()


Thanks
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dump class question

2006-11-12 Thread Alan Gauld
"johnf" <[EMAIL PROTECTED]> wrote 

> Myclass.py file contains:
> 
> Class one(object):
>  def needsomething(self):
> Class two (object):
>   def dosomething(self):
> 
> I want Class one's methods to access Class two methods?

Thats pretty wierd and would suggest a problem in your class 
design. Can you elaborate on why you think that would be 
necessary?

What is more common is for a method to want to access 
another object's methods and that is achieved by either 
passing the object in as an argument to the method or 
by having the instance stored in the class (possibly as a 
part of initialisation)

> Class one(object):
>  def needsomething(self):
>return dosomething()

  return self.aTwo.dosomething()

Or 

def needsomething(self,aTwo):
   return aTwo.dosomething()


Do either of those scenarions meet your needs?

It is possible to access a class's methods without instantiating 
the class but the results would be "interesting" in this case I 
suspect.


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] church numerals (fwd)

2006-11-12 Thread Alan Gauld
> From: Asrarahmed Kadri <[EMAIL PROTECTED]>

>> #
> def iszero(n):
>> ... def s(x):
>> ... return False
>> ... return n(s, True)
> 
> 
> Where is n() defined  ...?

n is a parameter of the function.

Thus to call iszero you need to pass in another function
which in turn takes a function and a boolean as its 
arguments.

Going back to Danny's original example:

>>> def zero(s, z):
... return z
...
>>> def one(s, z):
... return s(z)

Both of these "numbers" match the type of fuction that 
iszero expects. So we can do:

iszero(zero)

Now n takes on the vale of zero
and the return line of iszero becomes:

return zero(s,True)

and zero returns its second argument which is True, 
so zero is zero, as expected.

For iszero(one)

the return line is

return one(s,True)

but inside one the return is now

return s(True), but the return value of s is False.
So the ultimate reurtn value of iszero(one) is False, again 
as expected.

This is fairly mind bending the first time you come across it 
so don;t panic,ust work your way through a few more examples 
as I did above.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor