function & class

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Sun Feb 18 13:34:16 EST 2007


jupiter a écrit :
> hi friends,
> 
> I have a program like
> 
> some variable definations
> 
> a function ( arg1, agr2):
>           do something with arg1 & arg2
>           return a list
> 
> if condition
>    do this function(arg1,arg2)
> else
>   if condition
>     do this function
>    else
>       do this
> 
> 
> My problem is I want to use threading and I might need to pass values
> between function and classes.

which classes ? I see no class statement in your above pseudo-code.

> I am not sure how this can be done. I
> have read about classes and I know they are like function

Hmm. While technically, yes, Python classes are "callable" just like 
functions are, this is mostly an implementation detail (or at least you 
can consider it as such for now).  Now from a conceptual POV, functions 
and classes are quite different beasts.

> however does
> not return anything where as function does.

Where did you get such "knowledge", if I may ask ? Looks like you need a 
better source of informations !-)

> If I define class and then
> function in this class how do I access this function ?????

Classes are used to define and create ('instanciate' in OO jargon) 
objects. Usually, one first creates an object, then calls methods on 
this object:

class Greeter(object):
   def __init__(self, name, saywhat=None):
     self.name = name
     if self.saywhat is None:
       self.saywhat = "Hello %(who)s, greetings from %(name)s"
     else:
       self.saywhat = saywhat

   def greet(self, who):
     return self.saywhat % dict(name=self.name, who=who)


bruno = Greeter('Bruno')
print bruno.greet('Jupiter')

> I am not sure and confused about classes and  functions as how to go
> about them is there any simple way to understand difference between
> them

Read a tutorial on OO ? Here's one:
http://pytut.infogami.com/node11-baseline.html

> and when to use what and how to pass data/reference pointer
> between them ?

In Python, everything you can name, pass to a function or return from a 
function is an object.


> @nil
> Pythonist
> 



More information about the Python-list mailing list