def get_data(batchsize=None):
conn = psycopg2.connect('database parameter')
cursor = conn.cursor()
query = 'select * from table'
cursor.execute(query)
if not batchsize:
result = cursor.fetchall()
return result
else:
while True:
result = cursor.fetchmany(batchsize)
if not result:
break
yield result
if __name__ == '__main__':
data = get_data()
In above function type of data should have been a list as argument batchsize=None. but this function is returning generator in both cases. If I comment else part of the function then it's returning a list.