Last active
August 10, 2017 07:14
-
-
Save LeoQuote/724e4958898e3bdaa35fcbe45dc07fb9 to your computer and use it in GitHub Desktop.
In a class , called a generator outside the class ,but it didnt work, when you use a common function ,it works
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Thu Aug 10 14:43:04 2017 | |
@author: leoqu | |
""" | |
def dict_gen(curs): | |
field_names = ['id','name'] | |
while True: | |
if not curs: return | |
for row in curs: | |
print(row) | |
yield dict(zip(field_names, row)) | |
curs.remove(row) | |
class result_set: | |
def __init__(self,query=None): | |
self.query = None | |
self._result = [] | |
def _fetch_all(self): | |
items = [(1,'name1'),(2,'name2')] | |
# this won't work | |
for item in dict_gen(items): | |
print(item) | |
self._result += [item] | |
def get_result(self): | |
self._fetch_all | |
print(self._result) | |
def print_result(items): | |
result = [] | |
# this function will work | |
for item in dict_gen(items): | |
result += [item] | |
print(result) | |
print('output of a method inside class') | |
new_result_set = result_set() | |
new_result_set.get_result() | |
print('output of a common function') | |
print_result([(1,'name1'),(2,'name2')]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment