Python Forum

Full Version: class needs to refer a different class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
i have a class that, during __init__(), detects a situation where a different class, not itself, needs to be provided to the caller. but Python classes don't work by returning a reference to the class. is there a way to substitute an instance of a different class, instead?
Are you able to show approximately what are you trying to do with some basic classes?
You need to replace the call to the class by a call to a factory function.
I agree, a factory sounds more appropriate. Having a class know about its siblings seems like bad design because of coupling - you'll end up changing both classes when only one really needs to change and depending on the complexity, that can be hard to manage. So, instead, have a function that knows to create an instance of the right class based on whatever inputs are required. That logic is then kept out of any of the classes and is only changed in one place.
the classes are not siblings. the one being reference is in a module that is included in Python. there won't be changes. the "situation" being detected is the lack of need for my own class and the way it has been "changed". would a factory function still be the way to go? a factory function can be a class?
I'm confused about what you're trying to do then - perhaps code would help. The classes at least must share a common interface - or if not, how does it make sense to substitute one for another?

Sure, you can have a class that is a factory - the idea in general is really just to have a thing that is only responsible for the creation of the objects, so the places that use them don't have that logic. The classic Design Patterns book (by the "Gang of Four") explains the idea in more detail.
(Jul-16-2021, 06:19 AM)Yoriz Wrote: [ -> ]Are you able to show approximately what are you trying to do with some basic classes?
(Jul-17-2021, 07:42 PM)ndc85430 Wrote: [ -> ]I'm confused about what you're trying to do then

(Jul-17-2021, 07:42 PM)Yoriz Wrote: [ -> ]
(Jul-16-2021, 06:19 AM)Yoriz Wrote: [ -> ]Are you able to show approximately what are you trying to do with some basic classes?
i have no idea how to code this in Python.

(Jul-17-2021, 07:42 PM)ndc85430 Wrote: [ -> ]I'm confused about what you're trying to do then

other languages i have coded classes in had new method code that returned a reference, or pointer, to the new class instance. explaining this in Python terms would be a design change where __init__() would return self. in such a design it could return some other instance in its place, eliminating a need for this new object to "layer" the preferred object instead (as Python does things, now).

i have a new class, front-ended by a function named zopen() or ztopen(). if it detects that a normal file object is all that is needed for this instance, i want to have it so the basic file object is what the caller gets.
so you want to see code. this is big, so i am not going to post it here. instead, you get a link:
http://ipal.net/python/zopen.py [502 lines. 23122 bytes]
this code is not finished but it is working. here is a script that uses it:
http://ipal.net/python/zhash.py [131 lines, 5532 bytes]
and here is a generator that zhash uses.
http://ipal.net/python/ftrgen.py [59 lines, 2074 bytes]
sha256 is:
Output:
lt2a/forums /home/forums 19> zhash sha256 {ftrgen,zhash,zopen}.py 55f48a8f19d3d84daf54f90b3764ca13a7c2b24abfd8e6c63f5fe8f198719d2c *ftrgen.py 2d00f29d37a11f86c4d84b515cfe07f7e44df44d0833e81cb8ddf13544eb7662 *zhash.py b24200d214f211b2357aadbb6cf15c5564ea2b0ef70ac649e0b5e498c1fd1f18 *zopen.py lt2a/forums /home/forums 20>
(Jul-16-2021, 05:56 AM)Skaperen Wrote: [ -> ]i have a class that, during __init__(), detects a situation where a different class, not itself, needs to be provided to the caller. but Python classes don't work by returning a reference to the class. is there a way to substitute an instance of a different class, instead?
I think you are a more experienced programmer than I am but you seem to not want to just try things.
We don't need to see your full project files just some basic classes representing what you are trying to do.
I came up with the following based on your description it may be nothing like what you want but perhaps you can adjust it to give an indication of what you are after.
from dataclasses import dataclass


@dataclass
class AClass:
    situation: bool

    def __new__(cls, situation: bool):
        if situation:
            return DifferentClass(situation)
        return super().__new__(cls)


@dataclass
class DifferentClass:
    situation: bool


a_class = AClass(situation=False)
different_class = AClass(situation=True)
print(a_class)
print(different_class)
Output:
AClass(situation=False) DifferentClass(situation=True)
Pages: 1 2 3