How to import a variable from a different class

2024/10/13 10:27:12

I have an instance of a class that i set the value to 'self.world' inside a class named 'zeus' inside a module named 'Greek_gods'. and i have another class names 'World' inside a module name 'World'.

How can i tell zeus to go to 'World' and get the instance self.world?

#module named World
class World():def __init__(self):self.world = Atlas()#module named Greek_gods
class zeus():def __init__(self)
Answer

You can't, because you cannot know the world instance to be added in Zeus at the moment you create Zeus.

However, you can pass an instance of World for Zeus. There are various ways to do it:

  • Pass World as a parameter to the constructor:

    #module named greek_gods
    class Zeus(object):def __init__(self, world):self.world = Atlas()world = World()
    zeus = Zeus(world)
    
  • You can make World to create Zeus:

    #module named World
    class World(object):def __init__(self, world):self.world = Atlas()def create_zeus(self):zeus = Zeus()      # It would be better to pass it as a constructor zeus.world = world # parameter but let us leave it simple# Using your classes:world = World()zeus = World.create_zeus()
    
  • You can create one special instance of World in the world module:

    #module named World
    class World(object):def __init__(self):self.world = Atlas()
    greek_world = World()#module named Greek_gods
    import world
    class Zeus(object):def __init__(self):self.world = world.greek_world
    

I would recommend to use the second solution; after that, the first one. The third one, however, can be very useful, too, specially if, by default, you need just one world in your application (the so-called singletons).

https://en.xdnf.cn/q/118091.html

Related Q&A

Scrapy: AttributeError: YourCrawler object has no attribute parse_following_urls

I am writing a scrapy spider. I have been reading this question: Scrapy: scraping a list of links, and I can make it recognise the urls in a listpage, but I cant make it go inside the urls and save the…

initializer is not a constant, error C2099, on compiling a module written in c for python

i tried to compile a python module called distance, whith c "python setup.py install --with-c" using msvc 2017 on windows 10, i got this error ,Cdistance / distance.c (647): error C2099: init…

How can make pandas columns compare check cell?

I have a two file. a.txt has the below data.Zone,Aliase1,Aliase2 VNX7600SPB3_8B3_H1,VNX7600SPB3,8B3_H1 VNX7600SPBA_8B4_H1,VNX7600SPA3,8B4_H1 CX480SPA1_11B3_H1,CX480SPA1,11B3_H1 CX480SPB1_11B4_H1,CX480S…

Flask argument of type _RequestGlobals is not iterable

When I tried to use Flask-WTForms, I followed these steps:from flask_wtf import Form from wtforms import StringField, PasswordField from wtforms.validators import DataRequired, Emailclass EmailPassword…

PumpStreamHandler can capture the process output in realtime

I try to capture a python process output via apache-commons-exec. But it looks like it wont print the output, the output is only displayed after I the python process is finished.Heres my java codeComma…

Freezing a CNN tensorflow model into a .pb file

Im currently experimenting with superresolution using CNNs. To serve my model Ill need to frezze it first, into a .pb file, right? Being a newbie I dont really know how to do that. My model basically …

flattening a list or a tuple in python. Not sure what the error is

def flatten(t):list = []for i in t:if(type(i) != list and type(i) != tuple):list.append(i)else:list.extend(flatten(i))return listHere is the function that Ive written to flatten a list or a tuple that …

Counting word occurrences in csv and determine row appearances

I have a csv file such as the following in one column. The symbols and numbers are only to show that the file does not just contain text. I have two objectives:count the number of occurrences of a wo…

Converting Python 3.6 script to .exe? [duplicate]

This question already has answers here:How can I convert a .py to .exe for Python?(8 answers)Closed 6 years ago.I would like to convert a .py file to an .exe. I am using Python 3.6. I already tried py…

Error while reading csv file in python

I tried to run following programme in ubuntu terminal but I am getting some error. But it is not giving any error in jupyter notebookFile "imsl.py", line 5 SyntaxError: Non-ASCII character \x…