Simple Newbie Question: What am I doing wrong? 
Author Message
 Simple Newbie Question: What am I doing wrong?

I have a file named mytest.py containing the following:

def yo():
    print "Bla"

In the interactive window I type:

Quote:
>>> import mytest
>>> mytest.yo

But I get back

<function yo at 0x00985378>

Why don't I get back "Bla"?

Neither does it work if I do:

   return "Bla"

Why not?

Thank you for your help.



Tue, 13 Jul 2004 03:35:49 GMT  
 Simple Newbie Question: What am I doing wrong?
Quote:

> I have a file named mytest.py containing the following:

> def yo():
>     print "Bla"

> In the interactive window I type:

> >>> import mytest
> >>> mytest.yo

> But I get back

> <function yo at 0x00985378>

> Why don't I get back "Bla"?

Because the expressions you typed in is a reference to the function, not a
call of it. A call would be as follows:

    mytest.yo()

Remember, python is neither Perl nor VBScript, and because it makes just as
much sense for program to deal with functions as it does for them to deal
with character strings, you have to be able to both refer to and call
functions. It's quite legal to say this:

    ff = mytest.yo
    ff()

and this, too, should print "Bla".

Quote:
> Neither does it work if I do:

>    return "Bla"

I presume you mean that you replaced the print statement in the function
with a return?

Quote:
> Why not?

For the same reason :-)

regards
 Steve
--
Consulting, training, speaking: http://www.holdenweb.com/
Python Web Programming: http://pydish.holdenweb.com/pwp/



Tue, 13 Jul 2004 03:43:46 GMT  
 Simple Newbie Question: What am I doing wrong?

Quote:

>I have a file named mytest.py containing the following:

>def yo():
>    print "Bla"

>In the interactive window I type:

>>>> import mytest
>>>> mytest.yo

>But I get back

><function yo at 0x00985378>

>Why don't I get back "Bla"?

>Neither does it work if I do:

>   return "Bla"

>Why not?

>Thank you for your help.

See if I can get in first with the answer.

You're not calling the function unless you put () on the end.

So you need:

mytest.yo()

Otherwise you're just referencing the function. This is comon in a few
languages I can think of.

Why would you want to reference the function?

Consider:

func = mytest.yo
.
.
func()

Would also execute your function. Passing functions around is quite
handy sometimes.

--
Dale Strickland-Clark
Riverhall Systems Ltd



Tue, 13 Jul 2004 03:44:22 GMT  
 Simple Newbie Question: What am I doing wrong?


Quote:

> > I have a file named mytest.py containing the following:

> > def yo():
> >     print "Bla"

> > In the interactive window I type:

> > >>> import mytest
> > >>> mytest.yo

> > But I get back

> > <function yo at 0x00985378>

> > Why don't I get back "Bla"?

> Because the expressions you typed in is a reference to the function, not a
> call of it. A call would be as follows:

>     mytest.yo()

> Remember, Python is neither Perl nor VBScript, and because it makes just
as
> much sense for program to deal with functions as it does for them to deal
> with character strings, you have to be able to both refer to and call
> functions. It's quite legal to say this:

>     ff = mytest.yo
>     ff()

> and this, too, should print "Bla".

> > Neither does it work if I do:

> >    return "Bla"

> I presume you mean that you replaced the print statement in the function
> with a return?

> > Why not?

> For the same reason :-)

> regards
>  Steve
> --
> Consulting, training, speaking: http://www.holdenweb.com/
> Python Web Programming: http://pydish.holdenweb.com/pwp/

Oh! I see now.  Thank you very much.


Tue, 13 Jul 2004 03:53:13 GMT  
 Simple Newbie Question: What am I doing wrong?

Quote:

> In the interactive window I type:
>   >>> import mytest
>   >>> mytest.yo

> But I get back
>   <function yo at 0x00985378>

> Why don't I get back "Bla"?

Because you're not calling the function.

Quote:
>>> mytest.yo()

Bla

## Jason Orendorff    http://www.jorendorff.com/



Tue, 13 Jul 2004 03:39:05 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Stupid newb question: What am I doing wrong?

2. what am i doing wrong? HLA question

3. Where am I wrong?(this has to be wrong, it can't be so simple)

4. What am I doing wrong!

5. What am I doing wrong?

6. What am I doing Wrong

7. what am i doing wrong

8. Help - What am I doing wrong

9. Saving a Variable to a field in a record- what am i doing wrong

10. What am I doing wrong?

11. What am I doing wrong?

12. #CONTROL What am I doing wrong

 

 
Powered by phpBB® Forum Software