Let us assume that the open() member function is going to be useful in the future - perhaps it's going to get some more code that does something after it runs checks().
Here is how you code operates:
- Creates a Check instance and passes in 'Alfoo'
__init__
sets the member variable and calls open()
open()
calls check()
check()
counts the AEIOU and returns the count (variable a)
- the call to
check()
in open()
does not assign the return value so the value is lost
- the call to
open()
from __init__()
does not return a value so the __init__()
is done
- you then print the instance of Check
If I interpret what you are trying to do correctly, I would modify the code to this:
class Check():
def __init__(self, string):
# set member vaiables
self.string = string
self.count = None
# call member functions to initiate the instance properly
self.open()
# member function to 'open' something
def open(self):
if self.count is None:
self.checks()
# accessor (read-only) function for the member variable count
def get_count(self):
# we want to make sure that the count is set to a number
if self.count is None:
self.checks()
return self.count
# perform validation and set member variables
# so far, just sets the count member variable
def checks(self):
self.count = 0
for letter in self.string:
if letter == "a" or letter == "A" or letter == "E" or letter == "e" or letter == "U" or letter == "u" or letter == "O" or letter == "o" or letter == "I" or letter == "i":
self.count += 1
a = Check("Alfoo")
print(a.get_count())