Home > Enterprise >  How to appropriate a function into a class in Python
How to appropriate a function into a class in Python

Time:04-13

I am new to programming. In Coursera, I learnt to write a script that decides whether a sentence is palindrome or not. But it does not have a class. I need to appropriate this function into one with a class and self:

def is_palindrome(input_string):

new_string = ""
reverse_string = ""

for letter in input_string:
     
    if not letter == " ":
        new_string = letter   new_string
        reverse_string = reverse_string   letter

if reverse_string.lower() == new_string.lower():
    return True
return False

print(is_palindrome("Never Odd or Even"))
print(is_palindrome("abc"))
print(is_palindrome("kayak"))

But when I tried to do something like this:

class cis_palindrome:
  def is_palindrome(self, string):
    self.string = string

    new_string = ""
    reverse_string = ""

    for letter in string:
        
        if not letter == " ":
            new_string = letter   new_string
            reverse_string = reverse_string   letter

    if reverse_string.lower() == new_string.lower():
        return True

print(is_palindrome("Never Odd or Even"))
print(is_palindrome("abc"))
print(is_palindrome("kayak"))

It doesn't work. And I really tried to understand the concept of self and init, but the thing is that I could not find any examples in which self is replaced by the string that is not assigned before, and printed with the new input, like in these print examples.

CodePudding user response:

Your problem is NOT self and __init__ but you simply use class in wrong way.

First you have to create instance of class

instance = cis_palindrome()

and later use it

print( instance.is_palindrome(...) )

Eventually you can do it in one line - but this can be useful when you have to use it only once

print( cis_palindrome().is_palindrome(...) )

Full working code:

You don't use self.string so I removed it.

There is good rule to use CamelCaseNames for classes - it helps to recognize class in code.

class CisPalindrome:   # PEP8: `CamelCaseNames` for classes

  def is_palindrome(self, string):
    
    new_string = ""
    reverse_string = ""

    for letter in string:
        
        if not letter == " ":
            new_string = letter   new_string
            reverse_string = reverse_string   letter

    if reverse_string.lower() == new_string.lower():
        return True

instance = CisPalindrome()

print(instance.is_palindrome("Never Odd or Even"))
print(instance.is_palindrome("abc"))
print(instance.is_palindrome("kayak"))

PEP 8 -- Style Guide for Python Code


EDIT:

If you want to use self and __init__ then you can do

class CisPalindrome:
    
    def __init__(self, string):
        self.string = string
        
    def is_palindrome(self):

        new_string = ""
        reverse_string = ""

        for letter in self.string:
            if not letter == " ":
                new_string = letter   new_string
                reverse_string = reverse_string   letter

        if reverse_string.lower() == new_string.lower():
            return True

print(CisPalindrome("Never Odd or Even").is_palindrome())
print(CisPalindrome("abc").is_palindrome())
print(CisPalindrome("kayak").is_palindrome())

BTW:

Lines

    if reverse_string.lower() == new_string.lower():
        return True

You can reduce to

    return reverse_string.lower() == new_string.lower()

because == gives True or False

CodePudding user response:

You need to initialize your class to be able to use its methods (in your case, the is_palindrome method).

class cis_palindrome:

  def is_palindrome(self, string):
    # Your algorithm

# Initialization of the cis_palindrome class
pal = cis_palindrome()

# Call the is_palindrome method
result = pal.is_palindrome("kayak")
print(result)
  • Related