Python Forum
reference from another function
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reference from another function
#1
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
Reply
#2
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'))
Frankduc likes this post
Reply
#3
A big Thank you to you!
Now i have a better understanding of how it works.
Reply
#4
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'))
Reply
#5
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()
Reply
#6
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.
ndc85430 and Frankduc like this post
Reply
#7
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'
Reply
#8
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.
Frankduc likes this post
Reply
#9
As usual you're inputs are very useful.
else eliminated and str as well.
Thank you
Reply
#10
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]
Frankduc likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,572 Sep-07-2020, 08:02 AM
Last Post: perfringo
  Mathplotlib - passing reference to axs to function qmfoam 5 2,977 Aug-17-2020, 09:02 PM
Last Post: qmfoam
  reference in pop function for linked list oloap 0 1,582 Mar-14-2020, 05:52 PM
Last Post: oloap

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020