Python Forum

Full Version: Possible to create an object inside another object that is visible outside that objec
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I know it sounds confusing so I'll try to illustrate the question

in Main:

self.myobj = myclass


self.myobj(parameters)
the object code

class myobj(object)

    def __init__(self, ...):

    def mymethod((self, ...):

         my2ndobjinfirstobj  = my2ndclass
Forgiving the mile of inaccuracy above, can main use my2ndobjinfirstobj as if it were created in main? or reference it somehow?
Instance attributes are referenced using the instance_name.attribute_name. It doesn't matter if the attribute is a string, and int, or an instance of a custom class that you created.
class A:
    def __init__(self):
        self.value = 5

class B:
    def __init__(self):
        self.a = A()

b = B()
print(b.a.value)
(May-24-2023, 01:10 AM)MeghansUncle2 Wrote: [ -> ]I know it sounds confusing so I'll try to illustrate the question
Please post a complete example. As it is, the code is not understandable, for example
self.myobj = myclass
 
 
self.myobj(parameters)
cannot run because myclass has not been previously defined and parameters has not been defined, and also self has not been defined. Try to write code that we can run.
(May-24-2023, 06:11 AM)Gribouillis Wrote: [ -> ]
(May-24-2023, 01:10 AM)MeghansUncle2 Wrote: [ -> ]I know it sounds confusing so I'll try to illustrate the question
Please post a complete example. As it is, the code is not understandable, for example
self.myobj = myclass
 
 
self.myobj(parameters)
cannot run because myclass has not been previously defined and parameters has not been defined, and also self has not been defined. Try to write code that we can run.

This is not actual code. It is an abstract form of code to illustrate my question.

The question is, can i use an object created within another object outside of that other object. in my example, can i access object "B" outside of object "A"

Main Code
Self.A = Aclass

Self.A .somemethod
...
A class
class Aclass(object):

def somemethod()
       self.B = Bclass
I know I left out a lot and this code will not stand on its own. I am asking is there a way to reference the B object from the main code (outside of the A object)?
Your examples are more baffling than illustrative. Is somemethod() supposed to be a method of Aclass? In your example it is a function, not a method of Aclass. An example should be valid Python code.
class Bclass:
    pass

class Aclass:
    def somemethod(self):
         self.B = Bclass

obj_a = Aclass()
obj_a.B   # Will raise an AttributeError because B does not exist yet.
obj_a.somemethod()  # This creates B
obj_a,B  # Returns Bclass.
The answer is that an object attribute is an object attribute. The type of the attribute does not matter.
So close this out. I already said it was an abstract that was incomplete and incorrect in form. I want to know if I can do something, not if my very pseudo code is functional. Perhaps I am not asking the question correctly. I'll look elsewhere. Thanks.
(May-24-2023, 03:15 PM)MeghansUncle2 Wrote: [ -> ]I want to know if I can do something, not if my very pseudo code is functional.
We are only trying to understand the question. Besides your pseudo code, all that we have is the title of the thread, but it does not explain what you want to do. What is an object inside an object? Do you mean a member of this object? In Python, all the internal structures are "visible from outside", in the sense that there is no real access control to the variables, so what does the question mean?
The question means: I have a class that i want to instantiate and use by passing it to at least two other objects. But I don't want to instantiate that class in the main body, i would rather instantiate the class (create the object) inside another completely different object (possibly in its _init_ and share that instance with a completely seperate object.
Perhaps you mean this
class A:
    pass

class B:
    def __init__(self, spam):
        self.spam = spam

class C:
    def __init__(self, eggs):
        self.eggs = eggs

a = A() # instantiate A
b = B(a)
bb = B(a)
c = C(a)

# objects b, bb and c all share the same A instance
assert b.spam is bb.spam and b.spam is c.eggs
Quote:The question means: I have a class that i want to instantiate and use by passing it to at least two other objects. But I don't want to instantiate that class in the main body, i would rather instantiate the class (create the object) inside another completely different object (possibly in its _init_ and share that instance with a completely seperate object.
This is certainly possible, but it is problematic.

let's say that you have three object (a, b and c) that all want to know about the same object which well call the "golden snitch". a creates the golden snitch. b and c somehow have to be told that the snitch exists. How can this be done?

1. Create a first. When creating b and c, pass them the golden snitch.
a = ClassA()
b = ClassB(a.golden_snitch)
c = ClassC(a.golden_snitch)
The problem with this approach is all three classes need to know about the golden snitch. If you make changes to the snitch you will have to change ClassA, ClassB and ClassC. Your main program also has to know about the snitch, and it has to know the snitch is created by ClassA and has to be passed as an argument when making instances of ClassB and ClassC.

This approach works, but it has a lot of inter-dependency issues.

2. A slightly better approach is to not have ClassA make the snitch, but rather do that in the main program. You might have a reason to avoid doing this (you said you have a reason), but it does make for a better design because you removed the dependency that ClassA has to be created first, and now you can create ClassB and ClassC objects without having to make a ClassC object.
snitch = GoldenSnitch()
b = ClassB(snitch)  # Couldn't do this using pattern #1
3. A better approach yet is to not have any of the classes know about the snitch. In this pattern we write the program so the main program is the only one that knows about the snitch, the ClassA, ClassB and ClassC objects talk to the snitch through the main program (or a proxy created by the main program).
def catch_snitch(player):
    snitch.catch(player)

a = ClassA()
a.catch_function = catch_snitch
b = ClassB()
b.catch_function = catch_snitch
c = ClassC()
c.catch_function = catch_snitch
snitch = GoldenSnitch()
ClassA knows it has to call some function, but it does not know anything about the function other than it passes a "player" argument. Objects can be created in any order, including the Snitch.
a = ClassA()
b = ClassB()
c = ClassC()
snitch = GoldenSnitch()
a.connect(snitch.catch)
b.connect(snitch.catch)
c.connect(snitch.catch)
And there are other approaches that provide different degrees of isolation along with different amounts of work to achieve that isolation.

4. A slight variation of pattern 3. is to have the snitch provide the function that gets called.
Pages: 1 2