Python Forum
class needs to refer a different class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
class needs to refer a different class
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Are you able to show approximately what are you trying to do with some basic classes?
Reply
#3
You need to replace the call to the class by a call to a factory function.
ndc85430 likes this post
Reply
#4
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.
Reply
#5
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
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.
Reply
#7
(Jul-16-2021, 06:19 AM)Yoriz Wrote: Are you able to show approximately what are you trying to do with some basic classes?
Reply
#8
(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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
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>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
(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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  is ValueError a class? Skaperen 11 2,456 Mar-29-2023, 02:25 AM
Last Post: Skaperen
  extracting a function/class/method from code Skaperen 5 2,154 Mar-30-2022, 12:13 AM
Last Post: Skaperen
  first class objects Skaperen 0 924 Jan-22-2022, 02:53 AM
Last Post: Skaperen
  returning a different class Skaperen 4 2,069 Oct-20-2021, 12:51 AM
Last Post: Skaperen
  getting my head arounnd __enter__() for my new class Skaperen 5 2,470 Nov-30-2020, 09:46 AM
Last Post: Gribouillis
  find the class for indexed counting Skaperen 4 1,951 Sep-29-2020, 03:26 AM
Last Post: Skaperen
  namespaces when defining a class Skaperen 3 2,076 Jul-03-2020, 06:34 PM
Last Post: Gribouillis
  a file-like class implementation Skaperen 2 1,988 Apr-22-2020, 02:59 AM
Last Post: Skaperen
  making a generator class? Skaperen 2 2,035 Apr-01-2020, 12:34 AM
Last Post: Skaperen
  single-instance class Skaperen 3 2,450 Mar-05-2020, 12:47 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020