Search code examples
python-3.xobjectiterationdata-handling

How to use function inside for loop of another function into python


I want to use function inside function but I get error I want to get previous object,current object and next object how can I define that can anyone help me ? Actually I am new into the programming Thank you in advance

class DataHandler:
    def previous_and_next(some_iterable):
        prevs, items, nexts = tee(some_iterable, 3)
        prevs = chain([None], prevs)
        nexts = chain(islice(nexts, 1, None), [None])
        return zip(prevs, items, nexts)



    def current_obj(self):
        print("The current object is : ",employee_list[0])

    def next_obj(se):
        property = previous_and_next(employee_list)
        for previous, item, nxt in property:
            print("Item is now", item, "next is", nxt, "previous is", previous)

employee_list = []
n = int(input("Enter number of Employee you want to add:"))
for i in range(n):
    fname = input("Enter First name : ")
    lname = input("Enter Last name : ")
    emp_type = input("Enter Employee type : ")
    details = [fname,lname,emp_type]
    print(details)
    employee_list.append(details)


obj = DataHandler()
obj.current_obj()
obj.next_obj()

I am getting error at : property = previous_and_next(employee_list) NameError: name 'previous_and_next' is not defined


Solution

  • Well you can define function by self. Just add self in all functions and then you can use:

    def previous_and_next(self,some_iterable):
            prevs, items, nexts = tee(some_iterable, 3)
            prevs = chain([None], prevs)
            nexts = chain(islice(nexts, 1, None), [None])
            return zip(prevs, items, nexts)
    

    Property = self.previous_and_next(employee_list)