[PYTHON] Get only the subclass elements in a list

class Base(object):

	def getAttributeKeys(self):
		"""Defined in a class that inherits Base
element(Methods and properties)I want to get only the key of
		"""


class Child(Base):

	def __init__(self):
		self.hoge = "hoge"
	
	def fuga(self):
		pass

In this suitable example,

ch = Child()
print(ch.getAttributeKeys())   # >>> ['hoge', 'fuga']

When you want to be like.

Subtract using set

http://stackoverflow.com/questions/7136154/python-how-to-get-subclasss-new-attributes-name-in-base-classs-method I think there are various ways to do it, but I wonder if this way is personally beautiful.

Set operations are easy to use in Python, so use this Subclass element list minus parent class element list You can get a list of child classes like this.

class Base(object):

	def getAttributeKeys(self):
		return set(dir(self.__class__)) - set(dir(Base))

(* The result will be set instead of list.)

This and that

ch = Child()
keys = getAttributeKeys()


#Try to get the value of attribute
key = keys[0] #Suppose you choose from the keys
attr = ch.getattr(self, key)


#Determine if it is a method and execute
import types
if isinstance(attr, types.MethodType):
	#Since attr is a method, it can be executed
	attr()

Please use it when you want to automatically import what is defined in the subclass.

Recommended Posts

Get only the subclass elements in a list
Get the number of specific elements in a python list
How to get the last (last) value in a list in Python
Create a new list by combining duplicate elements in the list
Get the EDINET code list in Python
[Python] Get a list of folders only
[Python] Get the files in a folder with Python
Get the caller of a function in Python
Make a copy of the list in Python
[Linux] Command to get a list of commands executed in the past
Sort list elements in a specified order in Python
[Python] Manipulating elements in a list (array) [Sort]
How to get a list of files in the same directory with python
Get the file name in a folder using glob
Get the size (number of elements) of UnionFind in Python
[Python] Outputs all combinations of elements in the list
Group by consecutive elements of a list in Python
Methods available in the list
Getting list elements in Python
Get the value of a specific key in a list from the dictionary type in the list with Python
[Python] Leave only the elements that start with a specific character string in the array
How to check in Python if one of the elements of a list is in another list
Make sure all the elements in the list are the same in Python
Sort and output the elements in the list as elements and multiples in Python.
Developed a library to get Kindle collection list in Python
How to get a list of built-in exceptions in python
Get the number of occurrences for each element in the list
Get the value of a specific key up to the specified index in the dictionary list in Python
Recursively get the Excel list in a specific folder with python and write it to Excel.
I get a UnicodeDecodeError in mecab-python3
[python] Manage functions in a list
I get a KeyError in pyclustering.xmeans
Get the desktop path in Python
Get the script path in Python
Get the desktop path in Python
Get the host name in Python
Delete multiple elements in python list
When creating a matrix in a list
Get the query string (query string) in Django
Get a Boolean in Flask's request
Output the key list included in S3 Bucket to a file
How to compare lists and retrieve common elements in a list
Try to get a list of breaking news threads in Python.
How to get all the possible values in a regular expression
[Python] Display only the elements of the list side by side [Vertical, horizontal]
[python] Get the rank of the values in List in ascending / descending order
How to get the vertex coordinates of a feature in ArcPy
Determine if all list elements are present in the dict key
How to get a list excluding elements whose index is i ...?
Create a function to get the contents of the database in Go
Get the formula in an excel file as a string in Python
Get a list of files in a folder with python without a path
Get the number of readers of a treatise on Mendeley in Python
Get the client's IP address in Django
Memorandum (Add name only to people with the same surname in the list)
Get a capture of the entire web page in Selenium Python VBA
Get the top nth values in Pandas
Put together consecutive values in the list
Display a list of alphabets in Python 3
Get a list of packages installed in your current environment with python
Get the longest character length contained in an interval in a segment tree