Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to make a module iterable in Python?

I am making a module that I want to treat as a static container of objects. These objects are of a class type that I have defined. I want to be able to import this module and then loop over the objects within. Here is some code explaining what I mean:

example.py

class MyExampleClass(object):
    def __init__(self, var1, var2, var3):
        self.var1 = var1
        self.var2 = var2
        self.var3 = var3
        self.var4 = var4

instanceA = MyExampleClass(1, 2, 3, 4)
instanceB = MyExampleClass(4, 3, 6, 7)
instanceC = MyExampleClass(5, 3, 4, 5)

# something like this
def __iter__():
    return (instanceA, instanceB, instanceC)

Then I would like to be able to import this and use it like an enum:

import example

for e in example:
   # do stuff with e

Is this possible to do in Python? Or will I have to import a list from within the example package?

example.py

objects = (instanceA, instanceB, instanceC)

and then

import example

for e in example.objects:
    # do stuff with e
like image 974
Brian Schlenker Avatar asked Sep 15 '15 19:09

Brian Schlenker


People also ask

How do you make iterable in Python?

Create an Iterator To create an object/class as an iterator you have to implement the methods __iter__() and __next__() to your object. As you have learned in the Python Classes/Objects chapter, all classes have a function called __init__() , which allows you to do some initializing when the object is being created.

How do you make a class object iterable in Python?

To make a class as iterable, you have to implement the __iter__() and __next__() methods in that class. The __iter__() method allows us to make operations on the items or initializing the items and returns an iterator object.

How do you iterate over an object in Python?

Iterator in python is an object that is used to iterate over iterable objects like lists, tuples, dicts, and sets. The iterator object is initialized using the iter() method. It uses the next() method for iteration. next ( __next__ in Python 3) The next method returns the next value for the iterable.

What types are iterable in Python?

Examples of iterables include all sequence types (such as list , str , and tuple ) and some non-sequence types like dict , file objects, and objects of any classes you define with an __iter__() method or with a __getitem__() method that implements Sequence semantics.


2 Answers

You can achieve that by defining a root class in your module and replacing the module with an instance of that class. Here is an example:

class ModuleClass(object):
    __init__(self):
        self.instanceA = MyExampleClass(1, 2, 3, 4)
        ...

    __iter__(self):
        # Your iterator logic here

# and then in the same module code
sys.modules[__name__] = ModuleClass()

So then you can do what you want, because when you import that module, it will actually be an instance of your custom iterable ModuleClass:

import example

for e in example:
   # do stuff with e
like image 108
bagrat Avatar answered Oct 21 '22 05:10

bagrat


Although I do not recommend it, you can build your own module by inheriting from types.ModuleType. You can then replace the original module with your custom class with sys.modules[__name__] = NewModule()

import types
import sys

class MyExampleClass(object):
    def __init__(self, var1, var2, var3, var4):
        self.var1 = var1
        self.var2 = var2
        self.var3 = var3
        self.var4 = var4

class MyModule(types.ModuleType):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.instanceA = MyExampleClass(1, 2, 3, 4)
        self.instanceB = MyExampleClass(4, 3, 6, 7)
        self.instanceC = MyExampleClass(5, 3, 4, 5)

    def __iter__(self):
        return iter([self.instanceA, self.instanceB, self.instanceC])

sys.modules[__name__] = MyModule("example") # Name of the module
like image 3
David Avatar answered Oct 21 '22 05:10

David