0

I am actually a little confused about making module import generalize. What I got here is a class shape. What I want to do is want to import corresponding file as a module based on some condition. What i'm trying to do is:

In Shape.py

class shape:
    def __init__(self, shape_id):
        if shape_id == '001':
            from shapes import triangle as imported_shape
        else:
            from shapes import square as imported_shape

In main.py:

from Shape import shape

sqaure = shape('002')
...

The project structure is:

Project
    |
      Shape.py
      main.py
      shapes
          |
            triangle.py
            square.py

but that does not seems to work as the import is made void after the __init__ function. Is there any way i can make this type of importing more generalized?

4
  • What is your project structure ? Commented Sep 11, 2017 at 5:27
  • imports stay in the scope of functions as any objects Commented Sep 11, 2017 at 5:29
  • @YaroslavSurzhikov updated question with project structure Commented Sep 11, 2017 at 5:30
  • @PRMoureu That seems to work, but what i want is to import the module when initializing an object so that I could pass on the id while initialization. Commented Sep 11, 2017 at 5:36

1 Answer 1

1

I can't reproduce your bug.

As test I've included similar method to both square and triangle modules, which prints square or triangle respectively, something like that:

def a():
    print('square')

I called it in __init__ of shape class and recieved expected output.

class shape:
    def __init__(self, shape_id):
        if shape_id == '001':
            from shapes import triangle as imported_shape
        else:
            from shapes import square as imported_shape

        imported_shape.a()

But if you want to use imported module somewhere elsewhere of __init__ - you should assing imported_shape to self:

class shape:
    def __init__(self, shape_id):
        if shape_id == '001':
            from shapes import triangle as imported_shape
        else:
            from shapes import square as imported_shape

        self.imported_shape = imported_shape

And after that - you can access your module in other methods of shape class:

def test(self):
    self.imported_shape.a()

According to your needs and python code standarts - it would be better to import shapes on top of your module and in __init__ do something like:

import shapes

class shape:
    def __init__(self, shape_id):
        if shape_id == '001':
            self.imported_shape = shapes.triangle
        else:
            self.imported_shape = shapes.square

OOP example:

Asuming that square and triangle have same-named classes:

from shapes.square import square
from shapes.triangle import triangle


class shape(square, triangle):
    def __init__(self, shape_id):
        if shape_id == '001':
            super(triangle, self).__init__()
        else:
            super(square, self).__init__()
Sign up to request clarification or add additional context in comments.

2 Comments

It works, but my doubt is, is it a good approach to assign an import to a class variable?
@MrPyCharm, well, thats not ideal, yeah. Better approach would be to inherit classes triangle and square in class shape and on __init__ call super(shapes.triangle, self).__init__() or super(shapes.square, self).__init__() according to id passed as param

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.