1

When I ran this code:

class Check():
    def __init__(self, string):
        self.string = string
        self.open()

    def open(self):
        self.checks()

    def checks(self):
        a = 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":
                a = a+1
        return a

a = Check("Alfoo")
print(a)

I get:

<__main__.Check object at 0x00000257F224A978>
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
M7md
  • 33
  • 10
  • Did you mean `a = Check("Alfoo").checks()`? – Tomerikoo Mar 04 '21 at 19:12
  • 1
    Using a class seems overkill. – Matthias Mar 04 '21 at 19:13
  • How can i make it a = Check("Alfoo") only – M7md Mar 04 '21 at 19:14
  • 1
    Does this answer your question? [How to print instances of a class using print()?](https://stackoverflow.com/questions/1535327/how-to-print-instances-of-a-class-using-print) – Random Davis Mar 04 '21 at 19:14
  • Why? Calling the class creates a new object and that (usually) all it should do. If you want to call some functionality it should be explicitly called on the object – Tomerikoo Mar 04 '21 at 19:15
  • Out of curiosity, why do you have `init()` call `open()` and `open()` call `checks()`? It seems odd to use a class for this and not set it as an attribute rather than a chain of methods – G. Anderson Mar 04 '21 at 19:15
  • 1
    Get rid of the class and make `def checks(string)` and call `a = checks("Alfoo")`. Class is completely unnecessary. – Mark Tolonen Mar 04 '21 at 19:16

2 Answers2

1

First of all, there's no need to call self.open() from the constructor and then checks() from open(), you can remove that.

Inside the checks() function iterate over each letter in self.string and if the letter contains aeiou, keep on incrementing 1 in variable a, and then just return a.

class Check:
    def __init__(self, string):
        self.string = string

    def checks(self):
        a = sum(1 for letter in self.string.lower() if letter in 'aeiou')
        return a


a = Check("Alfoo")
print(a.checks())
Asad Sheikh
  • 61
  • 1
  • 8
  • You could eliminate the open function and the call to self.open() in __init__()... that would be simpler. – Sid Kwakkel Mar 04 '21 at 19:18
  • checks could also `return sum(1 for l in self.string if l in 'AEIOUaeiou')` but thats not a huge difference at all – Jab Mar 04 '21 at 19:30
  • While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – dan1st Mar 07 '21 at 07:39
  • I agree with you. The answer should have some sort of explanation for the readers. Also sorry, as I'm new to answering Stack Overflow questions and I will put some sort of explanation of my code in the future, and thank you for pointing it out. – Asad Sheikh Mar 07 '21 at 09:12
0

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:

  1. Creates a Check instance and passes in 'Alfoo'
  2. __init__ sets the member variable and calls open()
  3. open() calls check()
  4. check() counts the AEIOU and returns the count (variable a)
  5. the call to check() in open() does not assign the return value so the value is lost
  6. the call to open() from __init__() does not return a value so the __init__() is done
  7. 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())
Sid Kwakkel
  • 749
  • 3
  • 11
  • 31