Python Forum

Full Version: reference from another function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi,

How is it possible to use an if statement from another function. I am suppose to determine from the other function def inverserChaine(self): if it is a palindrome or not. I cant access the if condition from def inverserChaine(self):.

class Palindrome:
    '''Palindrome'''

    def __init__(self):
        self.chaine = ''

    def inverserChaine(self):

        for carac in self.chaine:
            inversion = carac + inversion
            return inversion

    def estUnPalindrome(self):

        if inversion == self.chaine:
            O = "C'est un palindrome"
            return O
        else:
            N = 'Ce n\'est pas un palindrome'
            return N

monPal = Palindrome()
Thank you
This would be one way to do it:
class Palindrome:
	'''Palindrome'''
 
	def inverserChaine(self, chaine):
 
		inversion = ''
		for carac in chaine:
			inversion = carac + inversion
		return inversion
 
	def estUnPalindrome(self, chaine):

		if self.inverserChaine (chaine) == chaine:
			O = "C'est un palindrome"
			return O
		else:
			N = 'Ce n\'est pas un palindrome'
			return N
 
monPal = Palindrome()
print (monPal.estUnPalindrome('abba'))
A big Thank you to you!
Now i have a better understanding of how it works.
To make some correction in language and style,like it' s not functions but methods that belong to the class Palindrome.
CamelCase🐫 look at PEP-8 and a song🎵 that make it clear.
The it will be like this.
class Palindrome:
    '''Palindrome'''
    def inverser_chaine(self, chaine):
        '''A method in class Palindrome'''
        inversion = ''
        for carac in chaine:
            inversion = carac + inversion
        return inversion

    def est_palindrome(self, chaine):
        '''A method in class Palindrome'''
        if self.inverser_chaine(chaine) == chaine:
            return "C'est un palindrome"
        return "Ce n'est pas un palindrome"

    @staticmethod
    def bar():
        '''
        Now can call bar a function placed in class Palindrome
        knows nothing about the class or instance
        '''
        return 100

mon_pal = Palindrome()
print (mon_pal.est_palindrome('abba'))
LOL crazy way to teach coding.

Good info. Thank you

Ultimatly i came up with this:
As long as it respect teacher's demands.

import tkinter as tk


class Palindrome:
    '''Palindrome'''

    def inverserChaine(self, chaine):

        inversion = ''
        for carac in chaine:
            inversion = carac + inversion
        return inversion

    def estUnPalindrome(self, chaine):

        if self.inverserChaine(chaine) == chaine:
            O = "C'est un palindrome"
            return O
        else:
            N = 'Ce n\'est pas un palindrome'
            return N

def tester():
    monPal = Palindrome()
    monPal.chaine = str(entD1.get())
    txtPal.configure(text='Es-ce une palindrome? ' + str(monPal.estUnPalindrome(monPal.chaine)))

fen = tk.Tk()
fen.title('Palindrome')

txtD1 = tk.Label(fen, text = 'Entrez votre mot ici:')
txtD1.grid(row = 0, column = 0, sticky = tk.E)

entD1 = tk.Entry(fen)
entD1.grid(row = 0, column = 1)

bouton = tk.Button(fen, text = "Tester", command = tester)
bouton.grid(row = 3, column = 2, columnspan = 2)

txtPal = tk.Label(fen, text = 'Es-ce une palindrome?')
txtPal.grid(row = 3, column = 0, columnspan = 2, sticky = tk.W)

fen.mainloop()
Stop doing this:
        if inversion == self.chaine:
            O = "C'est un palindrome"
            return O
        else:
            N = 'Ce n\'est pas un palindrome'
            return N
First off you should not use one letter variables. Second, variables should be snake_case with no upper case letters. Third, what is the purpose of "O" or "N"? Why assign these variables at all? Finally, if you return out of an if/else, why do you have the else?
        if inversion == self.chaine:
            return "C'est un palindrome"
        return 'Ce n\'est pas un palindrome'
And why are you doing this?
txtPal.configure(text='Es-ce une palindrome? ' + str(monPal.estUnPalindrome(monPal.chaine)))
What does Palindrome.estUnPalindrome() return? Does it return a str? If it returns a str, why are you doing str(str)?

You need to think about what you are typing. Be deliberate. Be concise.
dean,

I change it for :

        if self.inverserChaine(chaine) == chaine:
            return "C'est un palindrome"
        else:
            return 'Ce n\'est pas un palindrome'
You need to reference the first method otherwise its not working. Not on my side.

This part:
txtPal.configure(text='Es-ce une palindrome? ' + str(monPal.estUnPalindrome(monPal.chaine)))
Return the phrase 'Is this a palindrome?' and next to it return 'Yes it is' or 'No, Its not'
This code:
if self.inverserChaine(chaine) == chaine:
    return "C'est un palindrome"
else:
    return 'Ce n\'est pas un palindrome'
does not need (should not have) the else:
if self.inverserChaine(chaine) == chaine:
    return "C'est un palindrome"  # Will exit function with return value "C'est un palindrome"
return 'Ce n\'est pas un palindrome'  # Will exit function with return value  'Ce n\'est pas un palindrome'
This part:
str(monPal.estUnPalindrome(monPal.chaine))
does not need to call str() to convert monPal.estUnPalindrome() to a str. It is already a str.
As usual you're inputs are very useful.
else eliminated and str as well.
Thank you
You can use a plain function to solve the same problem.

conditional-expressions-pythons-ternary-operator
def est_un_palindrome(chaine):
        return  "C'est un palindrome" if chaine[::-1] == chaine else 'Ce n\'est pas un palindrome'
But in this example, the lines are too long. This is not readable.
Putting this snippet into the black auto formatter, gives me this result:

def est_un_palindrome(chaine):
    return (
        "C'est un palindrome"
        if chaine[::-1] == chaine
        else "Ce n'est pas un palindrome"
    )
If you have a str, you can reverse it with slicing.
This is also true for other sequences like tuple, list, dict (the insertion order is saved) but for example not things like frozenset or set. A set or frozenset do not have an order.
a_str = "anna1"
reversed_str = a_str[::-1]
Pages: 1 2