Python Forum
What are Palindromes in Python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What are Palindromes in Python?
#4
Hi again,
I realize my answer was silly.
Of course, palindromes can be implemented as a python class.
For instance, a very basic sample :
'''The palindrome module is a very basic implementation of palindromes.
Provided as the palindrome.py file'''
class Palindrome():
	'''The Palindrome class provides three public members functions:
		Palindrome(n) : the instanciation member, where n is an integer
		isPalindrome() : this function returns True is the class was instantiated with a palindrome integer
		__str__() : provides a printable form of the palindrome
	'''
	def __init__(self,candidate):
		self.value = candidate
	def isPalindrome(self):
		pa = list(str(self.value))
		pa.reverse()
		pn = int(''.join(pa))
		if pn == self.value:
			return True
		return False
	def __str__(self):
		if self.isPalindrome:
			return '%s is a palindrome' % self.value
		else:
			return '%s is not a palindrome' % self.value
This can be used by the following:
import palindrome
for i in (10,11,12):
	obj = palindrome.Palindrome(i)
	if obj.isPalindrome():
		print(obj)
The result is :
$ python3 pldrm.py
11 is a palindrome

And the docstring is available by:
$python3
Python 3.5.3 (default, Jan 19 2017, 14:11:04)
[GCC 6.3.0 20170118] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import palindrome
>>> help(palindrome)
>>> help(palindrome.Palindrome)
You can invent more complex functions... probably not very useful, but so fun
Reply


Messages In This Thread
What are Palindromes in Python? - by HarshaliPatel - Dec-08-2018, 06:33 AM
RE: What are Palindromes in Python? - by buran - Dec-08-2018, 07:14 AM
RE: What are Palindromes in Python? - by jeanMichelBain - Dec-09-2018, 04:16 PM
RE: What are Palindromes in Python? - by snippsat - Dec-09-2018, 10:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Help with Python - Palindromes SantiagoPB 4 2,321 Apr-04-2021, 06:09 PM
Last Post: buran
  Palindromes in Python karansingh 2 2,309 May-08-2019, 07:17 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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