return None

X

Ximo

Can I do a function which don't return anything?

The question is that, if I do a function that have a return or without
return, it returns always "None", but i want that it doesnt return me
nothing

Thanks
 
G

geon

Ximo said:
Can I do a function which don't return anything?

The question is that, if I do a function that have a return or without
return, it returns always "None", but i want that it doesnt return me
nothing

Nothing is None, or isnt?

You probably print the function, what you neednt, of course. You can just call it:

def x():
pass

print x() # calling with printing
x() # calling only

x() prints really nothing, even not None ;-)
 
M

Michael Hoffman

Ximo said:
Can I do a function which don't return anything?

The question is that, if I do a function that have a return or without
return, it returns always "None", but i want that it doesnt return me
nothing

When you execute the statement

a = myfunction()

What do you want to be in a, if not None?
 
R

Roy Smith

Can I do a function which don't return anything?

The question is that, if I do a function that have a return or without
return, it returns always "None", but i want that it doesnt return me
nothing

What do you mean by "don't return anything", as oppossed to returning
None? Let's say I had one of those functions, and did:

foo = noReturnValue()
print foo

what would you expect (want?) to happen?
 
G

Grant Edwards

What do you mean by "don't return anything", as oppossed to returning
None? Let's say I had one of those functions, and did:

foo = noReturnValue()
print foo

what would you expect (want?) to happen?

One supposes that if noReturnValue() really didn't return
anything, then the above code would cause an exception.

Personally, I don't really like the idea that falling off the
botton of a function implicitly returns None. It's just not
explicit enough for me. My preference would be that if the
function didn't execute a "return" statement, then it didn't
return anyting and attempting to use a return value would be an
error.

I suppose there probably is code out there that depends on the
implicit "return None" at the end of every function, so it's
too late to change things now.
 
A

Andreas Kostyrka

Can I do a function which don't return anything?

The question is that, if I do a function that have a return or without
return, it returns always "None", but i want that it doesnt return me
nothing
Define nothing :)
None is the representation of nothing in Python ;)

Andreas
 
T

Terry Reedy

Ximo said:
Can I do a function which don't return anything?

No, if you mean like subroutines.
The question is that, if I do a function that have a return or without
return, it returns always "None", but i want that it doesnt return me
nothing

Why? An answer to that would help you maybe get better answers here.

Terry J. Reedy
 
F

Fuzzyman

Grant Edwards wrote:
[snip..]
Personally, I don't really like the idea that falling off the
botton of a function implicitly returns None. It's just not
explicit enough for me. My preference would be that if the
function didn't execute a "return" statement, then it didn't
return anyting and attempting to use a return value would be an
error.

I suppose there probably is code out there that depends on the
implicit "return None" at the end of every function, so it's
too late to change things now.

Yeah - I frequently rely on that behaviour, but purely our of laziness.

Best Regards,

Fuzzy
http://www.voidspace.org.uk/python

 
E

Erik Max Francis

Grant said:
Personally, I don't really like the idea that falling off the
botton of a function implicitly returns None. It's just not
explicit enough for me. My preference would be that if the
function didn't execute a "return" statement, then it didn't
return anyting and attempting to use a return value would be an
error.

That's not a bad idea. I tend to prefer self-documenting code whenever
possible anyway, so if I a method ends that I don't intend to have a
useful return value, I don't have a return statement with an argument,
and if I intend a method that returns a useful value to return one that
might be None, I do so expliclitly, rather than having the logic fall
off the end of the function, making it unclear what was intended in the
first place.
 
C

Christopher Subich

Grant said:
Personally, I don't really like the idea that falling off the
botton of a function implicitly returns None. It's just not
explicit enough for me. My preference would be that if the
function didn't execute a "return" statement, then it didn't
return anyting and attempting to use a return value would be an
error.

This is a bad idea. Classically, distinguishing between functions that
return things and functions that don't return things explicitly divides
the "callable object" space. From my CS101 class with its incredibly
exciting dive into the world of useless pseudocode, callables that
returned things were called 'functions' and callables that didn't were
called 'procedures'.

Some languages do make this distinction; QBASIC, for example, had
'gosub' separate from function calls.

What do you do for an early break from a function that still returns
not-even-None [ReallyNone], "return?" That looks and acts like a 'real'
return statement, and the distinction between
return-without-a-value-so-maybe-except and return-with-a-value is
suddenly magnified to real importance.

Further, and I consider this a truly damning case, look at decorater. A
naive "logging" decorator could be defined like this:

def logger(func):
def new_func(*args, **kwargs):
print '%s called with:' % func.__name__, args, kwargs
retval = func(*args,**kwargs)
print '%s returns:', retval
return retval
return new_func

This logger works without modification for both value and non-value
returning functions. Its output isn't quite as pretty for non-value
functions, but it works and the implementation is both simple and flexible.

With a function-schism, to keep its simple implementation 'logger' would
have to be rewritten as 'flogger' (same as current-logger, for use on
functions), and 'plogger' (for use on procedures). The downside here is
that if the function/method changed to or from a procedure, the
decorator would have to be switched.

Alternatively, the logger decorator could be longer and explicitly catch
the possible exception. But why should we have to write like that, for
a use-case that doesn't even represent a true error -- arguably not even
an exceptional case? Python's definitely not a B&D language, talk of
floggers aside.
 
R

Repton

geon said:
Nothing is None, or isnt?

A woodman was carrying a sack full of chopped wood on his back. His
sack was heavy and filled beyond its limit. The man, bent under his
bulky burden, was struggling not to drop any of the wood pieces as he
walked. However, the poor man couldn't avoid tripping over a stone on
the road and half of his load fell out of their precarious pile.
Another man happened to be passing by and saw the mishap.

`If I load those fallen pieces of wood back into your back sack, what
would you give me?' he asked.

`Nothing.' said the man carrying the wood.

`That's acceptable.' agreed the other man. He collected all the chopped
wood scattered on the road and crammed them back into the sack of the
woodman. When done, he asked for his payment. The woodman was baffled.

`I told you, I would give you nothing.' he said.

`Yes. And that's what I want. Nothing.' said the other, `Give me my
nothing!'

After some quarrel, the two men decided to let the qadi solve their
problem. Nasreddin Hodja was on duty at the time. He listened to both
men earnestly. Then he addressed the man who was expecting his payment
of nothing.

`My dear fellow, could you please lift the far right corner of that rug
on the floor and check what is underneath?' The man did as he was told
and looked under the rug.

`What do you see?' asked the Hodja.

`Nothing.' said the man.

'Well, there's your payment.' said the Hodja. 'Take it and go!'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top