0

I currently have a module, my_module, which contains classes.py:

from . import config
class Class1:
    @staticmethod
    def method_one(x)
        return x + config.constant

    @staticmethod
    def method_two(y)
        return x - config.constant

My problem is that I want to be able to have multiple instances of the module with different values for config.constant. Ideally, I'd have a new class called MyClass where the constant would be an instance variable, so that this would work:

instance1 = MyClass(5)
instance1.Class1.method_one(5) # 10
instance2 = MyClass(0)
instance2.Class1.method_one(5) # 5

Is there a way to do this without modifying the classes which are in my_module?

Sebastian Mendez
  • 2,859
  • 14
  • 25
  • 1
    I don't see a class here, and why are you trying to create multiple instances of a module instead of multiple instances of a class? – user2357112 Jul 17 '17 at 19:59
  • There's multiple classes which the module has, each of them use the module constant. I want to create a class which functions exactly the same as the module. That is, `my_module.some_class.some_method() = my_class.some_class.some_method()` if `my_class`'s constant instance variable equals `my_module`'s `config_constant` – Sebastian Mendez Jul 17 '17 at 20:06
  • There are no classes in what you've shown us, though. Based on how you seem to think you've shown us a class, and how you want to do stuff like `instance1.module_class1`, I think you may have misunderstood what a class actually is. – user2357112 Jul 17 '17 at 20:12
  • Edited the post, hopefully it makes more sense now. – Sebastian Mendez Jul 17 '17 at 20:24

1 Answers1

0

Make it a staticmethod.

class MyClass(object):

    @staticmethod
    def my_method(num):
        return num + num

Then you can use it from another file by doing(assuming the file is my_module.py:

import my_module
my_module.MyClass.my_method(2)
>>>4

my_module.py

def __init__(self, constant):
    self.constant = constant

def my_method(self, num):
    return self.constant + num

some_other_file.py

import my_module
MyClass = type('MyClass', (), my_module.__dict__)
my_instance = MyClass(3)
my_instance.my_method(4)
>>> 7
Cory Madden
  • 5,026
  • 24
  • 37
  • That's not what I want; I should've elaborated, but there's multiple class files contained within the module, and each of which use `config.constant`. I simply want to have a class which functions like the module, only can be instantiated with a different constant value. – Sebastian Mendez Jul 17 '17 at 20:09
  • OK, I *think* I might understand, and it doesn't really make sense. A constant is a constant because it doesn't change. That's what constant means. If you want it to have it's own version of it, then you could set a property in the `__init__` method. like `def __init__(self, constant): self.constant = constant`. – Cory Madden Jul 17 '17 at 20:12
  • I think I'm having a tough time explaining it. In essence, I want to "classify" the module, as seen [here.](https://stackoverflow.com/a/16719281/4418475) However, in this class version of the module, I want to be able to instantiate a new class with a different value for the constant which all of the classes of the module use. – Sebastian Mendez Jul 17 '17 at 20:16
  • OK, OK. I think I finally get it. add an `__init__` method to your "class" file and do this. `MyClass = type('MyClass', (), my_module.__dict__)` then initialize it with `MyClass(3)` to set the constant for that file. Does that do it for you? – Cory Madden Jul 17 '17 at 20:22
  • I updated my original answer with a clearer explanation of what I mean. – Cory Madden Jul 17 '17 at 20:24
  • I edited the post to make it less confusing. Does your answer still fit what hopefully now makes more sense? – Sebastian Mendez Jul 17 '17 at 20:25
  • Kinda...maybe. I don't think you'll be able to do it without editing the original classes, though... – Cory Madden Jul 17 '17 at 20:31