0
**def colour_generator(lst):
    for i in lst:
        yield i
 print()
 colours=['red','green','blue']
 print(colour_generator(colours))**

here the funtion colour_generetor returns the object address but if in the same code we replace 'yield' with 'return' then it returns the first element in the list(lst).. Is 'yield' completely different from 'return' statement ?

Moreover **is it really possible to create object of a function?**As the output when 'return' statement is used instead of 'yield' statement in the above code is :"" or it is something else ?

ghdsjkf
  • 13
  • 2
  • Your description of observed behaviour shows that they are in fact different - the former returning a generator, the latter a value. – mhawke Apr 02 '16 at 11:43

1 Answers1

1

Your description of observed behaviour shows that they are in fact different - the former returning a generator, the latter a value.

is it really possible to create object of a function?

A function is an object of type/class function:

# Python 2
>>> f = lambda x: x
>>> type(f)
<type 'function'>

>>> def g(): pass
>>> type(g)
<type 'function'>

# Python 3
>>> f = lambda x: x
>>> type(f)
<class 'function'>
mhawke
  • 84,695
  • 9
  • 117
  • 138