python


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

Related Links

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

Categories

HOME
cakephp-3.x
udp
project-management
windows-7
visual-studio-2013
ios10
i2c
deeplearning4j
diagram
keystore
uiscrollview
ms-access-2007
multiple-columns
shippo
richfaces
azure-servicebus-queues
nmf
branch
emv
maximo
rowcount
division
modulo
nunit-3.0
bing-maps-api
wfp
concrete5-5.7
jrules
constants
pdftk
nstouchbar
workflow-foundation-4.5
jexl
efxclipse
sql-server-administration
cleditor
gmsmapview
proxygen
magento-1.4
altbeacon
multinomial
lemoon
jta
vips
wikitext
selendroid
kendo-menu
uialertview
castle-windsor-3
jquery-autocomplete
scala-2.11
errorprovider
adaptive-compression
httpcontext
indexing-service
help-viewer
json-patch
sim900
cling
fileconveyor
pys60
web-safe-fonts
tfs-power-tools
windows-phone-7-emulator
makefile-project
visual-c++-2008-express
inline-if
outlook-form
database-diagramming
associativity

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App