Python Forum
Possible to create an object inside another object that is visible outside that objec
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Possible to create an object inside another object that is visible outside that objec
#11
(May-24-2023, 05:37 PM)Gribouillis Wrote: 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

No, but that gave me an idea.

Is the below legal and will it expose the object created in A outside of A. Example below:

In the main program:

a = A(self) 
print(b)


Class A:
 def __init__(self, parentobject):
    parentobject.b = b() 
I appreciate your time answering this.
Reply
#12
(May-24-2023, 07:18 PM)MeghansUncle2 Wrote: Is the below legal and will it expose the object created in A outside of A. Example below:
Again it is difficult to understand the code that cannot run. It is legal to do parentobject.b = whatever
Reply
#13
This cannot be in the main program:
a = A(self) 
print(b)
self should only be used to refer to the instance variable passed as the first argument in an instance method. self should never bee seen as a variable in your main program.

This kind of thing is normally a bad idea.
Class A:
 def __init__(self, parentobject):
    parentobject.b = b() 
But that doesn't mean it is always a bad idea.

It would be better to do this:
class A:
 def __init__(self, b):
    parentobject.b = b

parentobject = ParentClass()
a = A(parentobject.b())
Instead of posting hypotheticals, why not post your real question?
Reply
#14
(May-24-2023, 07:03 PM)deanhystad Wrote:
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.

This I understand. Thanks for the information. I wanted the implementation for the object that I want to share to be done in isolation to the main but be usable by two other class objects. I don't really care where the code for it is but I don't want to have to repeatedly create the shared object that both of the consumers use every time I use the consumer classes. I appreciate the answers.
Reply
#15
In case you're interested, I created a class (call it Class A) that holds the consumer classes (Call them Class B and Class C) and creates the object that both classes depend upon. Then I just expose the required detail from Class B an class C through Class A back to whomever created the Class A object.

I hope this makes sense.
Reply
#16
(May-24-2023, 07:47 PM)deanhystad Wrote: Instead of posting hypotheticals, why not post your real question?


Two reasons.

1) Because I don't have any "real" code from which to base the question.
2) Learning concepts is far more important than actual code. Until I understand whats possible, I have no way to code an example properly.

Several times now I have posted questions on forums like this and the question often seems to garner more "why didn't you ask it the way I was expecting" instead of "this is what you need to understand about this issue" answers. To that, let me say this; If you think it's frustrating not to be able to understand the questions, imagine the frustration when you're new and don't yet understand how to ask it.
Reply
#17
It is best if you can write a working example that displays the problem you are trying to solve. I know I get confused about a question that has code that looks like Python but clearly is not Python. You are trying to ask a question in a language you don't understand, and the language barrier is a more difficult puzzle than the question.

I have a class that makes an object. I want to get that object from outside the class
class A:
    def __init__(self):
        self.thing = Thing()

a = A()
# outside_thing = ?
If you cannot write a snippet like that, it is probably better to just go with text.

I have a class (call it "A") that makes an object (call it "thing"). I want to access the "thing" from outside an instance of the class

If you don't know enough to ask a question like that, take it as an indication that you need to spend some time learning about classes and objects. Ask a question like this:

I am just getting started learning to write my own Python classes. Can you recommend a good resource?

After study you will know what classes, instances and attributes are, and know enough of language that there won't be a language barrier in asking your question. Of course, after some study you'll probably know the answer to your question.
Reply
#18
(May-25-2023, 01:29 PM)deanhystad Wrote: It is best if you can write a working example that displays the problem you are trying to solve. I know I get confused about a question that has code that looks like Python but clearly is not Python. You are trying to ask a question in a language you don't understand, and the language barrier is a more difficult puzzle than the question.

I have a class that makes an object. I want to get that object from outside the class
class A:
    def __init__(self):
        self.thing = Thing()

a = A()
# outside_thing = ?
If you cannot write a snippet like that, it is probably better to just go with text.

I have a class (call it "A") that makes an object (call it "thing"). I want to access the "thing" from outside an instance of the class

If you don't know enough to ask a question like that, take it as an indication that you need to spend some time learning about classes and objects. Ask a question like this:

I am just getting started learning to write my own Python classes. Can you recommend a good resource?

After study you will know what classes, instances and attributes are, and know enough of language that there won't be a language barrier in asking your question. Of course, after some study you'll probably know the answer to your question.

As you well know, the practical application of concepts learned in a book is something that takes some time and practice. Imagine that someone (insert my name here) has done so and still struggles with those concepts so he asks questions that are born of that inexperience and lack of understanding. Now imagine answering a question from such a person with "Go learn the language". It's a little frustrating.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing out incidence values for Class Object SquderDragon 3 305 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  This result object does not return rows. It has been closed automatically dawid294 6 1,091 Mar-30-2024, 03:08 AM
Last Post: NolaCuriel
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 416 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  How can I pause only one object? actualpy 1 371 Feb-01-2024, 07:43 PM
Last Post: deanhystad
  error in class: TypeError: 'str' object is not callable akbarza 2 526 Dec-30-2023, 04:35 PM
Last Post: deanhystad
Question Chain object that have parent child relation.. SpongeB0B 10 1,119 Dec-12-2023, 01:01 PM
Last Post: Gribouillis
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 762 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,773 Nov-07-2023, 09:49 AM
Last Post: buran
  TypeError: 'NoneType' object is not callable akbarza 4 1,025 Aug-24-2023, 05:14 PM
Last Post: snippsat
  [NEW CODER] TypeError: Object is not callable iwantyoursec 5 1,399 Aug-23-2023, 06:21 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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