Solved

Is my Python script following correct idiomatic Python format?

Posted on 2013-11-13
1
495 Views
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:

#!/usr/bin/python
class Person(object):
	def __init__(self, fname, lname, age, sex, weight):
		self.fname = fname
		self.lname = lname
		self.age = age
		self.sex = 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, self.sex))

	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

0
Comment
Question by:Tbalz
1 Comment
 
LVL 29

Accepted Solution

by:
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.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
winscp 000webhost.com 6 74
Delphi: barcode reading on android platform 1 51
Help Required 3 108
SMB share across internet 15 65
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

860 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