Accessing class attributes from a seperate class file
I'm a rookie so forgive me if this is obvious. I'm trying to access class attributes from a separate class file, as you can probably tell from the title. I run into a problem when calling the class. class Example: def __init__(self, test): self.test = test Say test is the attribute I wish to access. from test import Example class Example2: def __init__(self): self.test = Example() When I call example it says parameter test is unfilled. Let's pretend test already has an important value and I don't want to change it. What do I do in a situation like this and why?
You need to add a parameter for the Example() , Example("test").
The __init__ of Example takes an argument test which it assigns to class attribute test. So when you create an instance of Exmaple you need to provide this argument. The error parameter test is unfilled is I think a PyCharm warning which actually means that when you are calling self.test = Example() in def __init__ of Example2, you need to provide it a parameter. Regarding "Let's pretend test already has an important value and I don't want to change it": you are creating a new object with self.test = Example() and the test attribute in that object will not have any value. From the question summary, if you actually want to access the class attributes in another class, then you can create some getter functions in the class and use them in the other class. Or better make them properties. Read more here: https://docs.python.org/3/library/functions.html#property
Reinstall python 2.7.12 and python 3.5.2
How to keep chrome browser window open after selenium script finishes on python
Outlook email attachment downloader (Date range)
Create array based on conditional logic of values in other arrays in Python
Python qt - TableWidget update MySQL
Getting black plots with plt.imshow after multiplying image array by a scalar
Updating a value in a Pandas dataframe seems to update all dataframes
4 entry box nummeric keypad
os.walk for loop not executing [duplicate]
python matplotlib polar plot
Django - not showing properly one of my tables in template
Datalab: How to export Big Query standard SQL query to dataframe?
Pandas: count difference between dates
Skip one line in .csv file by using genfromtxt function in python
Dealing with NaNs in Pandas
Python Script? - Logging serial output from arduino