How to make mutual methods for different classes?












-2














I want to make a corpus of text (English and Ukrainian). I want to get the words of the corpus by passing the author to the method "words" via "Corpus.ENG_Lullabies". When I do that I get an error. I could make separate methods for both of the classes but the function is the same. But I want to save space, so I want to use a mutual method by inheriting it from another class. But I am doing something wrong.



Here is the code:



class Lullaby:
def __init__(self, title, author, words):
self.title = title
self.author = author
self.words = words

class Functions:
def __init__(self,Lullabies):
def words(author=''):
for i in Lullabies:
if author in Lullabies.author:
print(Lullabies.words)
self.words=words

class ENG():
def __init__(self):
Lullabies = [Lullaby('1','1',['word1','word2','word3']),Lullaby('2','2',['word4','word5','word6'])]
self.words=Functions(Lullabies).words


class UA():
def __init__(self):
Lullabies = [Lullaby('1','1',['Слово1','Слово2','Слово3']),Lullaby('2','2',['Слово4','Слово5','Слово6'])]
self.words = Functions(Lullabies).words

class Corpus():
def __init__(self):
self.ENG_Lullabies=ENG
self.UA_Lullabies=UA

cor=Corpus()
print(cor.ENG_Lullabies.words('1'))


Error:



Traceback (most recent call last):
File "D:/myproject/Lullaby Corpus/TEST.py", line 32, in <module>
print(cor.ENG_Lullabies.words('1'))
AttributeError: type object 'ENG' has no attribute 'words'


expected Output:



['word1','word2','word3']









share|improve this question
























  • UA doesn't inherit from Lullabies, so you can't use super(). Did you mean to inherit from Functions perhaps? You should not make words a nested function, however. It should just be a method, and the lullabies should be assigned as an attribute that words() would access on self.
    – Martijn Pieters
    Nov 21 '18 at 15:51












  • You seem to have forgotten to include the error you see. Please include the expected output, and what you get instead, full tracebacks for errors.
    – Martijn Pieters
    Nov 21 '18 at 15:53










  • Yes, I made a mistake, but super() was not the problem, that class wasn't even called
    – Igor Dragushhak
    Nov 21 '18 at 15:59






  • 1




    Well, you didn't assign instances to the Corpus instance. You assigned classes. You really should not be using a class just to create a nested function, however.
    – Martijn Pieters
    Nov 21 '18 at 16:00










  • I want to create a Corpus via a class (Corpus) that has two subcorpuses (ENG_Lullabies and UA_Lullabies), they both have a method "words" that returns the words by the author of the Lullaby.
    – Igor Dragushhak
    Nov 21 '18 at 16:06


















-2














I want to make a corpus of text (English and Ukrainian). I want to get the words of the corpus by passing the author to the method "words" via "Corpus.ENG_Lullabies". When I do that I get an error. I could make separate methods for both of the classes but the function is the same. But I want to save space, so I want to use a mutual method by inheriting it from another class. But I am doing something wrong.



Here is the code:



class Lullaby:
def __init__(self, title, author, words):
self.title = title
self.author = author
self.words = words

class Functions:
def __init__(self,Lullabies):
def words(author=''):
for i in Lullabies:
if author in Lullabies.author:
print(Lullabies.words)
self.words=words

class ENG():
def __init__(self):
Lullabies = [Lullaby('1','1',['word1','word2','word3']),Lullaby('2','2',['word4','word5','word6'])]
self.words=Functions(Lullabies).words


class UA():
def __init__(self):
Lullabies = [Lullaby('1','1',['Слово1','Слово2','Слово3']),Lullaby('2','2',['Слово4','Слово5','Слово6'])]
self.words = Functions(Lullabies).words

class Corpus():
def __init__(self):
self.ENG_Lullabies=ENG
self.UA_Lullabies=UA

cor=Corpus()
print(cor.ENG_Lullabies.words('1'))


Error:



Traceback (most recent call last):
File "D:/myproject/Lullaby Corpus/TEST.py", line 32, in <module>
print(cor.ENG_Lullabies.words('1'))
AttributeError: type object 'ENG' has no attribute 'words'


expected Output:



['word1','word2','word3']









share|improve this question
























  • UA doesn't inherit from Lullabies, so you can't use super(). Did you mean to inherit from Functions perhaps? You should not make words a nested function, however. It should just be a method, and the lullabies should be assigned as an attribute that words() would access on self.
    – Martijn Pieters
    Nov 21 '18 at 15:51












  • You seem to have forgotten to include the error you see. Please include the expected output, and what you get instead, full tracebacks for errors.
    – Martijn Pieters
    Nov 21 '18 at 15:53










  • Yes, I made a mistake, but super() was not the problem, that class wasn't even called
    – Igor Dragushhak
    Nov 21 '18 at 15:59






  • 1




    Well, you didn't assign instances to the Corpus instance. You assigned classes. You really should not be using a class just to create a nested function, however.
    – Martijn Pieters
    Nov 21 '18 at 16:00










  • I want to create a Corpus via a class (Corpus) that has two subcorpuses (ENG_Lullabies and UA_Lullabies), they both have a method "words" that returns the words by the author of the Lullaby.
    – Igor Dragushhak
    Nov 21 '18 at 16:06
















-2












-2








-2







I want to make a corpus of text (English and Ukrainian). I want to get the words of the corpus by passing the author to the method "words" via "Corpus.ENG_Lullabies". When I do that I get an error. I could make separate methods for both of the classes but the function is the same. But I want to save space, so I want to use a mutual method by inheriting it from another class. But I am doing something wrong.



Here is the code:



class Lullaby:
def __init__(self, title, author, words):
self.title = title
self.author = author
self.words = words

class Functions:
def __init__(self,Lullabies):
def words(author=''):
for i in Lullabies:
if author in Lullabies.author:
print(Lullabies.words)
self.words=words

class ENG():
def __init__(self):
Lullabies = [Lullaby('1','1',['word1','word2','word3']),Lullaby('2','2',['word4','word5','word6'])]
self.words=Functions(Lullabies).words


class UA():
def __init__(self):
Lullabies = [Lullaby('1','1',['Слово1','Слово2','Слово3']),Lullaby('2','2',['Слово4','Слово5','Слово6'])]
self.words = Functions(Lullabies).words

class Corpus():
def __init__(self):
self.ENG_Lullabies=ENG
self.UA_Lullabies=UA

cor=Corpus()
print(cor.ENG_Lullabies.words('1'))


Error:



Traceback (most recent call last):
File "D:/myproject/Lullaby Corpus/TEST.py", line 32, in <module>
print(cor.ENG_Lullabies.words('1'))
AttributeError: type object 'ENG' has no attribute 'words'


expected Output:



['word1','word2','word3']









share|improve this question















I want to make a corpus of text (English and Ukrainian). I want to get the words of the corpus by passing the author to the method "words" via "Corpus.ENG_Lullabies". When I do that I get an error. I could make separate methods for both of the classes but the function is the same. But I want to save space, so I want to use a mutual method by inheriting it from another class. But I am doing something wrong.



Here is the code:



class Lullaby:
def __init__(self, title, author, words):
self.title = title
self.author = author
self.words = words

class Functions:
def __init__(self,Lullabies):
def words(author=''):
for i in Lullabies:
if author in Lullabies.author:
print(Lullabies.words)
self.words=words

class ENG():
def __init__(self):
Lullabies = [Lullaby('1','1',['word1','word2','word3']),Lullaby('2','2',['word4','word5','word6'])]
self.words=Functions(Lullabies).words


class UA():
def __init__(self):
Lullabies = [Lullaby('1','1',['Слово1','Слово2','Слово3']),Lullaby('2','2',['Слово4','Слово5','Слово6'])]
self.words = Functions(Lullabies).words

class Corpus():
def __init__(self):
self.ENG_Lullabies=ENG
self.UA_Lullabies=UA

cor=Corpus()
print(cor.ENG_Lullabies.words('1'))


Error:



Traceback (most recent call last):
File "D:/myproject/Lullaby Corpus/TEST.py", line 32, in <module>
print(cor.ENG_Lullabies.words('1'))
AttributeError: type object 'ENG' has no attribute 'words'


expected Output:



['word1','word2','word3']






python class inheritance






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 15:54

























asked Nov 21 '18 at 15:46









Igor Dragushhak

587




587












  • UA doesn't inherit from Lullabies, so you can't use super(). Did you mean to inherit from Functions perhaps? You should not make words a nested function, however. It should just be a method, and the lullabies should be assigned as an attribute that words() would access on self.
    – Martijn Pieters
    Nov 21 '18 at 15:51












  • You seem to have forgotten to include the error you see. Please include the expected output, and what you get instead, full tracebacks for errors.
    – Martijn Pieters
    Nov 21 '18 at 15:53










  • Yes, I made a mistake, but super() was not the problem, that class wasn't even called
    – Igor Dragushhak
    Nov 21 '18 at 15:59






  • 1




    Well, you didn't assign instances to the Corpus instance. You assigned classes. You really should not be using a class just to create a nested function, however.
    – Martijn Pieters
    Nov 21 '18 at 16:00










  • I want to create a Corpus via a class (Corpus) that has two subcorpuses (ENG_Lullabies and UA_Lullabies), they both have a method "words" that returns the words by the author of the Lullaby.
    – Igor Dragushhak
    Nov 21 '18 at 16:06




















  • UA doesn't inherit from Lullabies, so you can't use super(). Did you mean to inherit from Functions perhaps? You should not make words a nested function, however. It should just be a method, and the lullabies should be assigned as an attribute that words() would access on self.
    – Martijn Pieters
    Nov 21 '18 at 15:51












  • You seem to have forgotten to include the error you see. Please include the expected output, and what you get instead, full tracebacks for errors.
    – Martijn Pieters
    Nov 21 '18 at 15:53










  • Yes, I made a mistake, but super() was not the problem, that class wasn't even called
    – Igor Dragushhak
    Nov 21 '18 at 15:59






  • 1




    Well, you didn't assign instances to the Corpus instance. You assigned classes. You really should not be using a class just to create a nested function, however.
    – Martijn Pieters
    Nov 21 '18 at 16:00










  • I want to create a Corpus via a class (Corpus) that has two subcorpuses (ENG_Lullabies and UA_Lullabies), they both have a method "words" that returns the words by the author of the Lullaby.
    – Igor Dragushhak
    Nov 21 '18 at 16:06


















UA doesn't inherit from Lullabies, so you can't use super(). Did you mean to inherit from Functions perhaps? You should not make words a nested function, however. It should just be a method, and the lullabies should be assigned as an attribute that words() would access on self.
– Martijn Pieters
Nov 21 '18 at 15:51






UA doesn't inherit from Lullabies, so you can't use super(). Did you mean to inherit from Functions perhaps? You should not make words a nested function, however. It should just be a method, and the lullabies should be assigned as an attribute that words() would access on self.
– Martijn Pieters
Nov 21 '18 at 15:51














You seem to have forgotten to include the error you see. Please include the expected output, and what you get instead, full tracebacks for errors.
– Martijn Pieters
Nov 21 '18 at 15:53




You seem to have forgotten to include the error you see. Please include the expected output, and what you get instead, full tracebacks for errors.
– Martijn Pieters
Nov 21 '18 at 15:53












Yes, I made a mistake, but super() was not the problem, that class wasn't even called
– Igor Dragushhak
Nov 21 '18 at 15:59




Yes, I made a mistake, but super() was not the problem, that class wasn't even called
– Igor Dragushhak
Nov 21 '18 at 15:59




1




1




Well, you didn't assign instances to the Corpus instance. You assigned classes. You really should not be using a class just to create a nested function, however.
– Martijn Pieters
Nov 21 '18 at 16:00




Well, you didn't assign instances to the Corpus instance. You assigned classes. You really should not be using a class just to create a nested function, however.
– Martijn Pieters
Nov 21 '18 at 16:00












I want to create a Corpus via a class (Corpus) that has two subcorpuses (ENG_Lullabies and UA_Lullabies), they both have a method "words" that returns the words by the author of the Lullaby.
– Igor Dragushhak
Nov 21 '18 at 16:06






I want to create a Corpus via a class (Corpus) that has two subcorpuses (ENG_Lullabies and UA_Lullabies), they both have a method "words" that returns the words by the author of the Lullaby.
– Igor Dragushhak
Nov 21 '18 at 16:06














1 Answer
1






active

oldest

votes


















3














You assigned classes to the Corpus attributes:



class Corpus():
def __init__(self):
self.ENG_Lullabies=ENG
self.UA_Lullabies=UA


but the rest of your code expected instances. Call classes to create instances:



class Corpus():
def __init__(self):
self.ENG_Lullabies = ENG()
self.UA_Lullabies = UA()


I strongly advice you to stick to Python's naming conventions, and use lower_case_with_underscores for attributes and local variables, so it is clearer when you have a class and when you should be working with an instance.



Next, you should use inheritance to pull in methods from Functions (which I'd rename here to better reflect the purpose of the base class).



You also want to separate presentation from class functionality; don't print in the words method. Return the results, and then, where it makes sense, use print() to turn those results into user feedback. That way you can later on also use the same method to, say, write the results to a network socket or a file, or show them in a GUI:



class Lullaby:
def __init__(self, title, author, words):
self.title = title
self.author = author
self.words = words

class LullabiesCollection:
def __init__(self, lullabies):
self.lullabies = lullabies

def words(self, author=''):
"""Return the words of lullabies, in a list

When author is given, limit the search to lullabies by author substring.

"""
result =
for lullaby in self.lullabies:
if not author or author in lullaby.author:
result.append(lullaby.words)
return result

class EnglishLullabies(LullabiesCollection):
def __init__(self):
super().__init__([
Lullaby('1', '1', ['word1', 'word2', 'word3']),
Lullaby('2', '2', ['word4', 'word5', 'word6'])
])

class UkranianLullabies(LullabiesCollection):
def __init__(self):
super().__init__([
Lullaby('1', '1', ['Слово1', 'Слово2', 'Слово3']),
Lullaby('2', '2', ['Слово4','Слово5','Слово6'])
])

class Corpus():
def __init__(self):
self.eng = EnglishLullabies()
self.ua = UkranianLullabies()

cor = Corpus()
for words in cor.eng.words('1'):
print(words)


Not that I think that Corpus, EnglishLullabies and UkranianLullabies actually need to be classes. Only create new classes if you are adding functionality; when just creating a grouping of lullabies, you don't need a new class.



The following would work too:



corpus = {
'eng': LullabiesCollection([
Lullaby('1', '1', ['word1', 'word2', 'word3']),
Lullaby('2', '2', ['word4', 'word5', 'word6'])
]),
'ua': LullabiesCollection([
Lullaby('1', '1', ['Слово1', 'Слово2', 'Слово3']),
Lullaby('2', '2', ['Слово4','Слово5','Слово6'])
]),
}
for words in corpus['eng'].words('1'):
print(words)





share|improve this answer























  • There is an error in the function words, as it is not a part of the init function it can't read self.lullabies
    – Igor Dragushhak
    Nov 21 '18 at 16:24












  • I found where is the problem. The words function needs to have self (def words(self, author=''))
    – Igor Dragushhak
    Nov 21 '18 at 16:32












  • Thank you, you've helped me a lot
    – Igor Dragushhak
    Nov 21 '18 at 16:32










  • @IgorDragushhak: Yes, sorry, I had indeed forgotten to add in the self parameter. Mea Culpa!
    – Martijn Pieters
    Nov 21 '18 at 17:47













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415726%2fhow-to-make-mutual-methods-for-different-classes%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














You assigned classes to the Corpus attributes:



class Corpus():
def __init__(self):
self.ENG_Lullabies=ENG
self.UA_Lullabies=UA


but the rest of your code expected instances. Call classes to create instances:



class Corpus():
def __init__(self):
self.ENG_Lullabies = ENG()
self.UA_Lullabies = UA()


I strongly advice you to stick to Python's naming conventions, and use lower_case_with_underscores for attributes and local variables, so it is clearer when you have a class and when you should be working with an instance.



Next, you should use inheritance to pull in methods from Functions (which I'd rename here to better reflect the purpose of the base class).



You also want to separate presentation from class functionality; don't print in the words method. Return the results, and then, where it makes sense, use print() to turn those results into user feedback. That way you can later on also use the same method to, say, write the results to a network socket or a file, or show them in a GUI:



class Lullaby:
def __init__(self, title, author, words):
self.title = title
self.author = author
self.words = words

class LullabiesCollection:
def __init__(self, lullabies):
self.lullabies = lullabies

def words(self, author=''):
"""Return the words of lullabies, in a list

When author is given, limit the search to lullabies by author substring.

"""
result =
for lullaby in self.lullabies:
if not author or author in lullaby.author:
result.append(lullaby.words)
return result

class EnglishLullabies(LullabiesCollection):
def __init__(self):
super().__init__([
Lullaby('1', '1', ['word1', 'word2', 'word3']),
Lullaby('2', '2', ['word4', 'word5', 'word6'])
])

class UkranianLullabies(LullabiesCollection):
def __init__(self):
super().__init__([
Lullaby('1', '1', ['Слово1', 'Слово2', 'Слово3']),
Lullaby('2', '2', ['Слово4','Слово5','Слово6'])
])

class Corpus():
def __init__(self):
self.eng = EnglishLullabies()
self.ua = UkranianLullabies()

cor = Corpus()
for words in cor.eng.words('1'):
print(words)


Not that I think that Corpus, EnglishLullabies and UkranianLullabies actually need to be classes. Only create new classes if you are adding functionality; when just creating a grouping of lullabies, you don't need a new class.



The following would work too:



corpus = {
'eng': LullabiesCollection([
Lullaby('1', '1', ['word1', 'word2', 'word3']),
Lullaby('2', '2', ['word4', 'word5', 'word6'])
]),
'ua': LullabiesCollection([
Lullaby('1', '1', ['Слово1', 'Слово2', 'Слово3']),
Lullaby('2', '2', ['Слово4','Слово5','Слово6'])
]),
}
for words in corpus['eng'].words('1'):
print(words)





share|improve this answer























  • There is an error in the function words, as it is not a part of the init function it can't read self.lullabies
    – Igor Dragushhak
    Nov 21 '18 at 16:24












  • I found where is the problem. The words function needs to have self (def words(self, author=''))
    – Igor Dragushhak
    Nov 21 '18 at 16:32












  • Thank you, you've helped me a lot
    – Igor Dragushhak
    Nov 21 '18 at 16:32










  • @IgorDragushhak: Yes, sorry, I had indeed forgotten to add in the self parameter. Mea Culpa!
    – Martijn Pieters
    Nov 21 '18 at 17:47


















3














You assigned classes to the Corpus attributes:



class Corpus():
def __init__(self):
self.ENG_Lullabies=ENG
self.UA_Lullabies=UA


but the rest of your code expected instances. Call classes to create instances:



class Corpus():
def __init__(self):
self.ENG_Lullabies = ENG()
self.UA_Lullabies = UA()


I strongly advice you to stick to Python's naming conventions, and use lower_case_with_underscores for attributes and local variables, so it is clearer when you have a class and when you should be working with an instance.



Next, you should use inheritance to pull in methods from Functions (which I'd rename here to better reflect the purpose of the base class).



You also want to separate presentation from class functionality; don't print in the words method. Return the results, and then, where it makes sense, use print() to turn those results into user feedback. That way you can later on also use the same method to, say, write the results to a network socket or a file, or show them in a GUI:



class Lullaby:
def __init__(self, title, author, words):
self.title = title
self.author = author
self.words = words

class LullabiesCollection:
def __init__(self, lullabies):
self.lullabies = lullabies

def words(self, author=''):
"""Return the words of lullabies, in a list

When author is given, limit the search to lullabies by author substring.

"""
result =
for lullaby in self.lullabies:
if not author or author in lullaby.author:
result.append(lullaby.words)
return result

class EnglishLullabies(LullabiesCollection):
def __init__(self):
super().__init__([
Lullaby('1', '1', ['word1', 'word2', 'word3']),
Lullaby('2', '2', ['word4', 'word5', 'word6'])
])

class UkranianLullabies(LullabiesCollection):
def __init__(self):
super().__init__([
Lullaby('1', '1', ['Слово1', 'Слово2', 'Слово3']),
Lullaby('2', '2', ['Слово4','Слово5','Слово6'])
])

class Corpus():
def __init__(self):
self.eng = EnglishLullabies()
self.ua = UkranianLullabies()

cor = Corpus()
for words in cor.eng.words('1'):
print(words)


Not that I think that Corpus, EnglishLullabies and UkranianLullabies actually need to be classes. Only create new classes if you are adding functionality; when just creating a grouping of lullabies, you don't need a new class.



The following would work too:



corpus = {
'eng': LullabiesCollection([
Lullaby('1', '1', ['word1', 'word2', 'word3']),
Lullaby('2', '2', ['word4', 'word5', 'word6'])
]),
'ua': LullabiesCollection([
Lullaby('1', '1', ['Слово1', 'Слово2', 'Слово3']),
Lullaby('2', '2', ['Слово4','Слово5','Слово6'])
]),
}
for words in corpus['eng'].words('1'):
print(words)





share|improve this answer























  • There is an error in the function words, as it is not a part of the init function it can't read self.lullabies
    – Igor Dragushhak
    Nov 21 '18 at 16:24












  • I found where is the problem. The words function needs to have self (def words(self, author=''))
    – Igor Dragushhak
    Nov 21 '18 at 16:32












  • Thank you, you've helped me a lot
    – Igor Dragushhak
    Nov 21 '18 at 16:32










  • @IgorDragushhak: Yes, sorry, I had indeed forgotten to add in the self parameter. Mea Culpa!
    – Martijn Pieters
    Nov 21 '18 at 17:47
















3












3








3






You assigned classes to the Corpus attributes:



class Corpus():
def __init__(self):
self.ENG_Lullabies=ENG
self.UA_Lullabies=UA


but the rest of your code expected instances. Call classes to create instances:



class Corpus():
def __init__(self):
self.ENG_Lullabies = ENG()
self.UA_Lullabies = UA()


I strongly advice you to stick to Python's naming conventions, and use lower_case_with_underscores for attributes and local variables, so it is clearer when you have a class and when you should be working with an instance.



Next, you should use inheritance to pull in methods from Functions (which I'd rename here to better reflect the purpose of the base class).



You also want to separate presentation from class functionality; don't print in the words method. Return the results, and then, where it makes sense, use print() to turn those results into user feedback. That way you can later on also use the same method to, say, write the results to a network socket or a file, or show them in a GUI:



class Lullaby:
def __init__(self, title, author, words):
self.title = title
self.author = author
self.words = words

class LullabiesCollection:
def __init__(self, lullabies):
self.lullabies = lullabies

def words(self, author=''):
"""Return the words of lullabies, in a list

When author is given, limit the search to lullabies by author substring.

"""
result =
for lullaby in self.lullabies:
if not author or author in lullaby.author:
result.append(lullaby.words)
return result

class EnglishLullabies(LullabiesCollection):
def __init__(self):
super().__init__([
Lullaby('1', '1', ['word1', 'word2', 'word3']),
Lullaby('2', '2', ['word4', 'word5', 'word6'])
])

class UkranianLullabies(LullabiesCollection):
def __init__(self):
super().__init__([
Lullaby('1', '1', ['Слово1', 'Слово2', 'Слово3']),
Lullaby('2', '2', ['Слово4','Слово5','Слово6'])
])

class Corpus():
def __init__(self):
self.eng = EnglishLullabies()
self.ua = UkranianLullabies()

cor = Corpus()
for words in cor.eng.words('1'):
print(words)


Not that I think that Corpus, EnglishLullabies and UkranianLullabies actually need to be classes. Only create new classes if you are adding functionality; when just creating a grouping of lullabies, you don't need a new class.



The following would work too:



corpus = {
'eng': LullabiesCollection([
Lullaby('1', '1', ['word1', 'word2', 'word3']),
Lullaby('2', '2', ['word4', 'word5', 'word6'])
]),
'ua': LullabiesCollection([
Lullaby('1', '1', ['Слово1', 'Слово2', 'Слово3']),
Lullaby('2', '2', ['Слово4','Слово5','Слово6'])
]),
}
for words in corpus['eng'].words('1'):
print(words)





share|improve this answer














You assigned classes to the Corpus attributes:



class Corpus():
def __init__(self):
self.ENG_Lullabies=ENG
self.UA_Lullabies=UA


but the rest of your code expected instances. Call classes to create instances:



class Corpus():
def __init__(self):
self.ENG_Lullabies = ENG()
self.UA_Lullabies = UA()


I strongly advice you to stick to Python's naming conventions, and use lower_case_with_underscores for attributes and local variables, so it is clearer when you have a class and when you should be working with an instance.



Next, you should use inheritance to pull in methods from Functions (which I'd rename here to better reflect the purpose of the base class).



You also want to separate presentation from class functionality; don't print in the words method. Return the results, and then, where it makes sense, use print() to turn those results into user feedback. That way you can later on also use the same method to, say, write the results to a network socket or a file, or show them in a GUI:



class Lullaby:
def __init__(self, title, author, words):
self.title = title
self.author = author
self.words = words

class LullabiesCollection:
def __init__(self, lullabies):
self.lullabies = lullabies

def words(self, author=''):
"""Return the words of lullabies, in a list

When author is given, limit the search to lullabies by author substring.

"""
result =
for lullaby in self.lullabies:
if not author or author in lullaby.author:
result.append(lullaby.words)
return result

class EnglishLullabies(LullabiesCollection):
def __init__(self):
super().__init__([
Lullaby('1', '1', ['word1', 'word2', 'word3']),
Lullaby('2', '2', ['word4', 'word5', 'word6'])
])

class UkranianLullabies(LullabiesCollection):
def __init__(self):
super().__init__([
Lullaby('1', '1', ['Слово1', 'Слово2', 'Слово3']),
Lullaby('2', '2', ['Слово4','Слово5','Слово6'])
])

class Corpus():
def __init__(self):
self.eng = EnglishLullabies()
self.ua = UkranianLullabies()

cor = Corpus()
for words in cor.eng.words('1'):
print(words)


Not that I think that Corpus, EnglishLullabies and UkranianLullabies actually need to be classes. Only create new classes if you are adding functionality; when just creating a grouping of lullabies, you don't need a new class.



The following would work too:



corpus = {
'eng': LullabiesCollection([
Lullaby('1', '1', ['word1', 'word2', 'word3']),
Lullaby('2', '2', ['word4', 'word5', 'word6'])
]),
'ua': LullabiesCollection([
Lullaby('1', '1', ['Слово1', 'Слово2', 'Слово3']),
Lullaby('2', '2', ['Слово4','Слово5','Слово6'])
]),
}
for words in corpus['eng'].words('1'):
print(words)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 17:47

























answered Nov 21 '18 at 16:15









Martijn Pieters

701k13224272266




701k13224272266












  • There is an error in the function words, as it is not a part of the init function it can't read self.lullabies
    – Igor Dragushhak
    Nov 21 '18 at 16:24












  • I found where is the problem. The words function needs to have self (def words(self, author=''))
    – Igor Dragushhak
    Nov 21 '18 at 16:32












  • Thank you, you've helped me a lot
    – Igor Dragushhak
    Nov 21 '18 at 16:32










  • @IgorDragushhak: Yes, sorry, I had indeed forgotten to add in the self parameter. Mea Culpa!
    – Martijn Pieters
    Nov 21 '18 at 17:47




















  • There is an error in the function words, as it is not a part of the init function it can't read self.lullabies
    – Igor Dragushhak
    Nov 21 '18 at 16:24












  • I found where is the problem. The words function needs to have self (def words(self, author=''))
    – Igor Dragushhak
    Nov 21 '18 at 16:32












  • Thank you, you've helped me a lot
    – Igor Dragushhak
    Nov 21 '18 at 16:32










  • @IgorDragushhak: Yes, sorry, I had indeed forgotten to add in the self parameter. Mea Culpa!
    – Martijn Pieters
    Nov 21 '18 at 17:47


















There is an error in the function words, as it is not a part of the init function it can't read self.lullabies
– Igor Dragushhak
Nov 21 '18 at 16:24






There is an error in the function words, as it is not a part of the init function it can't read self.lullabies
– Igor Dragushhak
Nov 21 '18 at 16:24














I found where is the problem. The words function needs to have self (def words(self, author=''))
– Igor Dragushhak
Nov 21 '18 at 16:32






I found where is the problem. The words function needs to have self (def words(self, author=''))
– Igor Dragushhak
Nov 21 '18 at 16:32














Thank you, you've helped me a lot
– Igor Dragushhak
Nov 21 '18 at 16:32




Thank you, you've helped me a lot
– Igor Dragushhak
Nov 21 '18 at 16:32












@IgorDragushhak: Yes, sorry, I had indeed forgotten to add in the self parameter. Mea Culpa!
– Martijn Pieters
Nov 21 '18 at 17:47






@IgorDragushhak: Yes, sorry, I had indeed forgotten to add in the self parameter. Mea Culpa!
– Martijn Pieters
Nov 21 '18 at 17:47




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415726%2fhow-to-make-mutual-methods-for-different-classes%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python