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

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]
Python Stringcompare
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

Categories

HOME
angular-cli
itext
blockchain
backup
wms
ubuntu-12.04
orientation
value
triggers
pygame
gimp
desktop
shipping
pca
outlook-web-addins
lagom
ssms-2016
zoomcharts
pyephem
ng-show
eclipselink
ng2-dragula
plsqldeveloper
statusbar
embedly
sensu
source-maps
tampermonkey
sumo
continuous-deployment
osmdroid
pptp
ab-initio
superagent
lmdb
rowcount
spreedly
swiftcharts
node-horseman
phpspreadsheet
powershell-remoting
sidr
rider
zero
topic-modeling
visualstudio.testtools
nashorn
spring-bean
encapsulation
windows-firewall
sgmlreader
pinvoke
livescribe
windows-95
mbaas
collapsingtoolbarlayout
date-range
qos
build-process
sql-import-wizard
pdflib
libreadline
icefaces
debugdiag
mix
fps
gradle-release-plugin
search-regex
financial
hornetq
matcaffe
anti-patterns
ytplayerview
criteria-api
coypu
shift-jis
sysfs
android-search
lexicographic
libsndfile
mser
dmp
firebug-lite
dto
picat
late-static-binding
broadcasting
xenocode
uipickerviewcontroller
jquery-tabs
angulartics
bigint
ng-hide
ienumerator
adomd.net
fraud-prevention
magickwand
fluidsynth
linkedhashset
qsqltablemodel
getusermedia
subtract
driving-directions
jquery-selectbox
querystringparameter
pureftpd
hadoop-plugins
delegatecommand
scala-2.8
trampolines
multibyte-functions
gears

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