Is my Python script following correct idiomatic Python format?

Posted on 2013-11-13
Last Modified: 2013-11-26
Hi Guys,

I'm new to program and I have written some Python code below to get a better understanding of the language. I have read that you always want to refractor your code and break it down into as much as you can and have the functions do one thing very well. Is my understanding correct?

Would my code style below be considered good Python code? Are there any bad habits that I have and should avoid? I want to learn Python the right was and avoid any bad habits that would be hard to shake later on. Any help will be greatly appreciated.

 My code is Below:

class Person(object):
	def __init__(self, fname, lname, age, sex, weight):
		self.fname = fname
		self.lname = lname
		self.age = age = sex
		self.weight = weight

	#The display functions are belows
	def display_person_name(self):
		return ("The name of the person is: %s %s" % (self.fname, self.lname))

	def display_person_age(self):
		return ("The age of the person is: %i"  % (self.age))

	def display_person_sex(self):
		return ("The sex of %s %s is: %s" % (self.fname, self.lname,

	def display_person_weight(self):
		return ("The weight of the person is: %i pounds" % (self.weight))

	def ask_person_fname(self):
		self.ask_fname = raw_input("What is your first name?: ")
		return ("Your first name is: %s" % (self.ask_fname))

	def ask_person_lname(self):
		self.ask_person_lname = raw_input("What is your last name?: ")
		return ("Your last name is: %s" % (self.ask_person_lname))

	def ask_person_age(self):
		self.ask_person_age = raw_input("What is your age: ")
		return ("Your age is: %s " % (self.ask_person_age ))

	def ask_person_sex(self):
		self.ask_person_sex = raw_input("What is your sex?: ")
		return ("Your sex is: %s " % (self.ask_person_sex))

	def ask_person_weight(self):
		self.ask_person_weight = raw_input("What is your weight: ")
		return ("Your weight is: %s " % (self.ask_person_weight))

if __name__ == "__main__":
	#Creating test object new object
	person1 = Person('Bob','Barker', 46, 'Male', 237)
	print person1.display_person_name()
	print person1.display_person_age()
	print person1.display_person_sex()
	print person1.display_person_weight()
	PlaceHolder_fname = person1.ask_person_fname()
	PlaceHolder_lname = person1.ask_person_lname()
	PlaceHolder_age = person1.ask_person_age()
	PlaceHolder_sex = person1.ask_person_sex()
	PlaceHolder_weight = person1.ask_person_weight
	#Creating new object with placeHolder variables being read in to initialize object
	person2 = Person( str(PlaceHolder_fname), str(PlaceHolder_lname), PlaceHolder_age, str(PlaceHolder_sex), int(PlaceHolder_weight))

Open in new window

Question by:Tbalz
1 Comment
LVL 28

Accepted Solution

pepr earned 500 total points
ID: 39647352
Minor problem: the return command does not require parentheses. It looks just strange.

Big problem: You should not duplicate the information. Your self.ask_person_weight (and the like) exists in parallel with self.weight (and the like).

Problem with naming the variables: Correct naming of variables would reveal the above problem with duplicate variables. Variables should never start with a werb. They are passive. They are just names for the content.

Names of method should suggest what they do -- think about person1.display_person_weight(). It actually does not display anything.

Any class (object) should support only the neccessary things related to the object itself. For example, the person class/object should not care about what is the sentence used for getting the information from the user.

For example,

      person1.weight = raw_input("What is your weight: ")
      print "Your weight is:", person1.weight

is (in my opinion) cleaner than introducing the ask_person_weight method and printing the result of the call.

Think about personification of the person1 object :)  Would you feel natural if an agent told you: "I would like to print the result of your ask_person_weight" ?

Update: I have just noticed a huge problem. You define a method with the name ask_person_weight. When the method is called, it is converted to the variable with the same name. It is possible, but it is definitely not what you want. I guess you have just overlooked the problem and you wanted to assign the self.weight.

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Squid Connection Pools 3 45
Rubik's Cube Code for Effective Presentation 3 40
nagios alerts 3 32
"Black Box" Testing of Control System Software 2 21
A short article about a problem I had getting the GPS LocationListener working.
Fine Tune your automatic Updates for Ubuntu / Debian
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now