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
Python Framework for Visualizing a Simulation [closed]
Python TypeError: unhashable type: 'list''
How do I link records to a large table efficiently using python Dedupe?
Find all posibililties in a dict (Python 2.7) [closed]
C++ Classes and Python Objects [duplicate]
How to use Java/Scala function from an action or a transformation?
Only ImportError when running file directly [duplicate]
Unable to install packages using pip in virtualenv
Django Render function: template not printing variable
Fastest way to set a dictionary value in python when greater than current key value
Returning values from dictionary in python
How do I print a range of lines after a specific pattern into separate files when this pattern appears several times in an input file
Get more result from Github search
Python : Speed up Reverse DNS lookup
Clean way of structuring ctypes class