Re: Pass and return

2012-12-21 Thread Steven D'Aprano
On Thu, 20 Dec 2012 21:23:58 -0800, iMath wrote:

 Pass and return
 Are these two functions the same ?

They are neither functions, nor are they the same.

Check if they are functions:

- can you pass them arguments?
- can you assign their result to a target?

No.

py pass(23)
  File stdin, line 1
pass(23)
^
SyntaxError: invalid syntax
py x = return
  File stdin, line 1
x = return
 ^
SyntaxError: invalid syntax


Are they the same? Try it with these two functions:

def test_pass():
for i in range(100):
pass
print i

def test_return():
for i in range(100):
return
print i

py test_pass()
99
py test_return()
py 


So what are they?

They are *statements*, not functions. You cannot pass them arguments, nor 
do they assign a result to a target on the left hand side of = equals 
sign.

pass is a do-nothing statement. It literally does nothing.

return exits a function and sets the return result. It is only legal 
inside functions and generators, while pass is legal almost anywhere. 
Normally you say return some_value, but you can leave out the result 
and Python will return None.

If functions get all the way to the bottom without a return statement, 
they will return None.


The example you give:

 def test():
   return

The body of the function immediately returns None. But functions return 
None by default, so you could leave the return statement out. If you do 
that, you will get a SyntaxError because there is nothing in the body:


py def test():
... 
... 
  File stdin, line 3

^
IndentationError: expected an indented block


So if you put a pass statement in, just to satisfy the compiler, you 
get the same result:


 def test():
   pass


Also a function which immediately exists and return None.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pass and return

2012-12-21 Thread Duncan Booth
Mitya Sirenef msire...@lightbird.net wrote:

 On 12/21/2012 12:23 AM, iMath wrote:
 Pass and return
 Are these two functions the same ?

 def test():
  return
   
 def test():
  pass
 
 
  From the point of style, of course, the latter is
 much better because that's the idiomatic way
 to define a no-op function. With a return, it
 looks like you might have forgotten to add the
 value to return or deleted it by mistake.
 
I would say it is *an* idiomatic way to define a no-op function.

Another idiomatic way is to use a doc-string as the only body, 
that way you can also explain why you feel the need for an empty 
function.

-- 
Duncan Booth http://kupuguy.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pass and return

2012-12-21 Thread Mitya Sirenef

On 12/21/2012 03:52 AM, Duncan Booth wrote:

Mitya Sirenef msire...@lightbird.net wrote:


On 12/21/2012 12:23 AM, iMath wrote:

Pass and return
Are these two functions the same ?

def test():
  return
   
def test():

  pass


  From the point of style, of course, the latter is
much better because that's the idiomatic way
to define a no-op function. With a return, it
looks like you might have forgotten to add the
value to return or deleted it by mistake.


I would say it is *an* idiomatic way to define a no-op function.

Another idiomatic way is to use a doc-string as the only body,
that way you can also explain why you feel the need for an empty
function.



That's true, a docstring is preferable in many cases.  -m


--
Lark's Tongue Guide to Python: http://lightbird.net/larks/

--
http://mail.python.org/mailman/listinfo/python-list


Pass and return

2012-12-20 Thread iMath
Pass and return
Are these two functions the same ?

def test():
return 
 
def test():
pass
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pass and return

2012-12-20 Thread Mitya Sirenef

On 12/21/2012 12:23 AM, iMath wrote:

Pass and return
Are these two functions the same ?

def test():
return
  
def test():

pass


I believe they are the same, but these statements have
different meanings in other circumstances, e.g.:

Class A(object): pass

def test():
  if x: return
  else: # do something

In first example, (in a class), return would be invalid.

In second example, return would return None from function,
pass would result in continuing execution after if/else block.

Btw you can use disassemble function to look into what
these functions do:

 def a(): pass
 def b():return
 from dis import dis
 dis(a)
  1   0 LOAD_CONST   0 (None)
  3 RETURN_VALUE
 dis(b)
  1   0 LOAD_CONST   0 (None)
  3 RETURN_VALUE


So indeed they should be the same..

 -m

--
Lark's Tongue Guide to Python: http://lightbird.net/larks/

--
http://mail.python.org/mailman/listinfo/python-list


Re: Pass and return

2012-12-20 Thread Mitya Sirenef

On 12/21/2012 12:23 AM, iMath wrote:

Pass and return
Are these two functions the same ?

def test():
return
  
def test():

pass



From the point of style, of course, the latter is
much better because that's the idiomatic way
to define a no-op function. With a return, it
looks like you might have forgotten to add the
value to return or deleted it by mistake.

 -m

--
Lark's Tongue Guide to Python: http://lightbird.net/larks/

--
http://mail.python.org/mailman/listinfo/python-list


Re: Pass and return

2012-12-20 Thread Chris Angelico
On Fri, Dec 21, 2012 at 4:23 PM, iMath redstone-c...@163.com wrote:
 Pass and return
 Are these two functions the same ?

 def test():
 return

 def test():
 pass

They're different statements, but in this case they happen to
accomplish the same thing.

The pass statement means do nothing. For instance:

while input(Enter 5 to continue: )!=5:
  pass

The return statement means stop executing this function now, and
return this value, or None if no value.

Running off the end of a function implicitly returns None.

So what you have is one function that stops short and returns None,
and another that does nothing, then returns None. The functions
accomplish exactly the same, as does this:

test = lambda: None

All three compile to the same short block of code - load the constant
None, and return it.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: call python from c - pass and return arrays/lists

2008-04-10 Thread Diez B. Roggisch
Pieter wrote:

 Hi all,
 
 I'm trying to call a python function from c. I need to pass an
 2D-array to python and the python function returns a 2D-list back to
 c. I googled arround and I found how to pass ints/strings/... back and
 forth, but didn't find anything on passing arrays.
 
 For an int it's as simple as this:
 
 PyArg_Parse(ret,i#, my_long);
 
 But I hacve no idea how to parse python lists to a c-array?

You return a list, parse that as object, and then work with the
sequence-protocol on them.

UNTESTED:

PyArg_Parse(ret,o, the_list);
if(PySequence_Check(the_list) {
  int size = PySequence_Size(the_list);
  int *result = malloc(sizof(int) * size);
  for(int i = 0; i  size; i++) {
 PyObject *item = PySequence_GetItem(the_list, i);
 if(PyInt_Check(item)) {
   result[i] = PyInt_AsLong(item);
 } else {
   return NULL;
 }
  }



Diez
-- 
http://mail.python.org/mailman/listinfo/python-list


call python from c - pass and return arrays/lists

2008-04-10 Thread Pieter
Hi all,

I'm trying to call a python function from c. I need to pass an
2D-array to python and the python function returns a 2D-list back to
c. I googled arround and I found how to pass ints/strings/... back and
forth, but didn't find anything on passing arrays.

For an int it's as simple as this:

PyArg_Parse(ret,i#, my_long);

But I hacve no idea how to parse python lists to a c-array?

thanks a lot,

Pieter
-- 
Pieter Cogghe
Ganzendries 186
9000 Gent
0487 10 14 21
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: call python from c - pass and return arrays/lists

2008-04-10 Thread Pieter
Thanks, this did the trick:

PyArg_Parse(ret,O, the_list);
if (PySequence_Check(the_list)) {
 int size = PySequence_Size(the_list);
 int *result = malloc(sizeof(int) * size);
 int i;
 for (i = 0; i  size; i++) {
PyObject *item = PySequence_GetItem(the_list, i);
if(PyInt_Check(item)) {
  result[i] = PyInt_AsLong(item);
  printf(value %d: %d, i, result[i]);
} else {
printf(Didn't work, NO INT);
}
 }
}

kind regards,

Pieter

-- Doorgestuurd bericht --
From: Diez B. Roggisch [EMAIL PROTECTED]
To: python-list@python.org
Date: Thu, 10 Apr 2008 12:23:56 +0200
Subject: Re: call python from c - pass and return arrays/lists
Pieter wrote:

 Hi all,

 I'm trying to call a python function from c. I need to pass an
 2D-array to python and the python function returns a 2D-list back to
 c. I googled arround and I found how to pass ints/strings/... back and
 forth, but didn't find anything on passing arrays.

 For an int it's as simple as this:

 PyArg_Parse(ret,i#, my_long);

 But I hacve no idea how to parse python lists to a c-array?

You return a list, parse that as object, and then work with the
sequence-protocol on them.

UNTESTED:

PyArg_Parse(ret,o, the_list);
if(PySequence_Check(the_list) {
 int size = PySequence_Size(the_list);
 int *result = malloc(sizof(int) * size);
 for(int i = 0; i  size; i++) {
PyObject *item = PySequence_GetItem(the_list, i);
if(PyInt_Check(item)) {
  result[i] = PyInt_AsLong(item);
} else {
  return NULL;
}
 }



Diez

-- 
Pieter Cogghe
Ganzendries 186
9000 Gent
0487 10 14 21
-- 
http://mail.python.org/mailman/listinfo/python-list