Re: [Tutor] If a method has no return type?

2013-02-07 Thread Sunil Tech
Thank you Wesley, Kal & tutor


On Thu, Feb 7, 2013 at 12:07 PM, Kal Sze  wrote:

> Dear Sunil,
>
> No method or function in Python has a *static* return type. That's
> because Python is by nature a dynamic language, with duck typing and
> dynamic dispatch. In fact, any method or function may well return any
> of a number of different types:
>
> def crazy_function(return_int)
> if return_int:
> return 1
> else:
> return 'foo'
>
> It's probably bad design, but there is nothing in the Python grammar
> and semantics that stops you from doing that.
>
> So your question is better phrased as: if I don't explicitly return
> anything, what is returned?
>
> The answer to that would be: the None object
>
> Cheers,
> Kal
>
> On 7 February 2013 14:09, Sunil Tech  wrote:
> > If a method has no return type?
> > what will it return?
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > To unsubscribe or change subscription options:
> > http://mail.python.org/mailman/listinfo/tutor
> >
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] If a method has no return type?

2013-02-07 Thread Steven D'Aprano

On 07/02/13 17:32, wesley chun wrote:

On Thu, Feb 7, 2013 at 12:09 AM, Sunil Tech  wrote:


If a method has no return type?
what will it return?



note the original question is partially invalid... Python functions and
methods aren't typed. however, i imagine the OP really meant *return value*
instead, so the answer is really the following:
functions *and* methods with no explicit return *value* return None... this
happens when you either have no return statement or just return by itself
with no object given.

one exception is __init__()... you're never supposed to return anything
from it... ever. i can't think of any others... can you?


In-place modification methods typically return None. Functions and methods
intended to operate as procedures, or via side-effect, also typically
return None:

list.sort
list.reverse
list.append
list.extend
dict.clear
dict.update
set.clear
set.update
__delitem__
__setitem__
_delattr__
__setattr__
random.shuffle
file.close
print  # Python 3 only

But as far as I know, __init__ is the only one which actually enforces the
rule that it returns None, at least in Python 3.3.



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] If a method has no return type?

2013-02-07 Thread Timo

Op 07-02-13 10:09, Sunil Tech schreef:

Thank you Wesley, Kal & tutor

In the future, the Python interpreter comes in handy for quick checks.

>>> def spam():
...   pass
...
>>> s = spam()
>>> s, repr(s), type(s)
(None, 'None', )

Timo




On Thu, Feb 7, 2013 at 12:07 PM, Kal Sze > wrote:


Dear Sunil,

No method or function in Python has a *static* return type. That's
because Python is by nature a dynamic language, with duck typing and
dynamic dispatch. In fact, any method or function may well return any
of a number of different types:

def crazy_function(return_int)
if return_int:
return 1
else:
return 'foo'

It's probably bad design, but there is nothing in the Python grammar
and semantics that stops you from doing that.

So your question is better phrased as: if I don't explicitly return
anything, what is returned?

The answer to that would be: the None object

Cheers,
Kal

On 7 February 2013 14:09, Sunil Tech mailto:sunil.tech...@gmail.com>> wrote:
> If a method has no return type?
> what will it return?
>
> ___
> Tutor maillist  - Tutor@python.org 
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor