I am learning Python and I would like to know how to "print", in a terminal, a table that is defined inside a class. This is the relevant code (and my attempt):
import re
import pandas as pd
import sys, os
import numpy as np
import nltk
import operator
import math
class Extractor():
def __init__(self,softskills,hardskills,jobdesc,cvdesc):
self.softskills=self.load_skills(softskills)
self.hardskills=self.load_skills(hardskills)
self.jb_distribution=self.build_ngram_distribution(jobdesc)
self.cv_distribution=self.build_ngram_distribution(cvdesc)
def makeTable(self):
#I am interested in verbs, nouns, adverbs, and adjectives
parts_of_speech=['CD','JJ','JJR','JJS','MD','NN','NNS','NNP','NNPS','RB','RBR','RBS','VB','VBD','VBG','VBN','VBP','VBZ']
graylist=["you", "will"]
tmp_table=[]
#look if the skills are mentioned in the job description and then in your cv
for skill in self.hardskills:
if skill in self.jb_distribution:
count_jb=self.jb_distribution[skill]
if skill in self.cv_distribution:
count_cv=self.cv_distribution[skill]
else:
count_cv=0
m1=self.measure1(count_jb,count_cv)
m2=self.measure2(count_jb,count_cv)
tmp_table.append(['hard',skill,count_jb,count_cv,m1,m2])
for skill in self.softskills:
if skill in self.jb_distribution:
count_jb=self.jb_distribution[skill]
if skill in self.cv_distribution:
count_cv=self.cv_distribution[skill]
else:
count_cv=0
m1=self.measure1(count_jb,count_cv)
m2=self.measure2(count_jb,count_cv)
tmp_table.append(['soft',skill,count_jb,count_cv,m1,m2])
print(tmp_table) # this command prints my table in the terminal
p = Extractor('softskills.txt','hardskills.txt','job.txt','cv.txt')
tbl = p.makeTable()
print(tbl) # this command does not show my table in the terminal
If I use the command print(tmp_table) inside def makeTable(self): (as shown in the above-mentioned code) I get the correct table displayed in the terminal, i.e.
[['hard', 'programming', 2, 2, 0, 0], ['hard', 'modelling', 18, 20, -2, 0], ['hard', 'linux', 1, 0, 1, 1], ['hard', 'analytical', 1, 0, 1, 1], ['hard', 'teamwork', 1, 0, 1, 1], etc...
…but if I try to print my table outside the class…
p = Extractor('softskills.txt','hardskills.txt','job.txt','cv.txt')
tbl = p.makeTable()
print(tbl)
…I get the following result:
None
Any idea ?
>Solution :
Your method does not return anything, therefore python will fall back to the default that tbl is None, i.e. a method, that has no explicit return statement, returns None, which is also your print out.
If you want the outer print(tbl) to print anything, you have to add a return tmp_table at the end of your method.
Simply printing it to the terminal is not a return in the sense of how programming works.