python-2.7


how to verify if variable exist in parent class in python


I have to add all the variables exist in parent class in one list in child class. However all the variables in the parent class is not mandatory.
Is there any way i can figure out if variable exist in parent class or not ??
>>> class A(object):
... pass
...
>>> class B(A):
... pass
...
>>> B.__bases__
(<class '__main__.A'>,)
>>> getattr(B.__bases__[0],'x',False)
False
Or more comprehensivley:
class A(object):
x = 5
class B(A):
y = 6
x = 7
print(getattr(B.__bases__[0],'y',False))
print(getattr(B.__bases__[0],'x',False))
Outputs
False
5
>>>
So just check wether or not False is returned, if it is then you can conclude the attribute doesn't exist in the parent class.
If you know what attributes you're looking for, you can match them against dir(parent class)

Related Links

How to assign current date to a date field in odoo v8?
How can I query Google App Engine entities by name to convert to new names?
How to set socks5 proxy in dryscrape python?
How does pyaramorph (Arabic morphological analyzer) work? [closed]
Convert UTC to local time
Python Kivy: Get Music hotkeys signal of phone/headphones
IPython - ipython_notebook_config.py missing
How to implicitly use the base definition of a method
Creat List of URLs for grequest
Create directory and write files to a remote, password protected computer
jinja2 encoding error in python
mysql.connector, multi=True, sql variable assignment not working
Geo Pandas Data Frame / Matrix - filter/drop NaN / False values
pip django-lfs gives error
Graph API image upload - permission issues
Why does “print ('\033[0m')” barf on Python 2.7.6 Ubuntu 14.04 LTS

Categories

HOME
weblogic12c
xcode
clearcase
yahoo-weather-api
opencv-python
paypal-ipn
bots
operating-system
tabs
qooxdoo
clone
anchor
documentum
mailgun
max-msp-jitter
twisted
vmware-workstation
mixpanel
bazaar
parceler
raml
adminlte
bsd
bosun
batch-rename
sybase-ase
offline-caching
network-analysis
steam-web-api
series
riotjs
java-stream
subclassing
uivisualeffectview
google-now
secure-coding
liteide
rightnow-crm
section508
django-static-precompiler
cordova-ios
network-protocols
apache-modules
polymaps
business-rules
hmmlearn
deviare
mathjs
pyaudio
coovachilli
conan
between
fiber
uibinder
urbit
django-redis
vimperator
outlook.com
google-maps-api-2
bjam
featuretoggle
apple
android-handler
algebraixlib
nsmutabledata
vs-unit-testing-framework
global-scope
libgcc
webmethod
xcode6.3.1
reference-counting
smips
delphi-xe4
web-frameworks
oscilloscope
tfs-sdk
reentrancy
ckeditor.net
gitx
playorm
popup-blocker
websphere-6.1
uipangesturerecognizer
infopath-2007
tessellation
cleartype
resharper-5.0
.net-client-profile
service-factory

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile