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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wipe a usb using python 5 48
linux(debian) mouse poor performance 4 42
Embarcadero C++ builder XE10.1 Berlin TRegistry declaration 1 29
android secure ftp 3 38
This is an explanation of a simple data model to help parse a JSON feed
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…

803 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