Solved

Is my Python script following correct idiomatic Python format?

Posted on 2013-11-13
1
489 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 28

Accepted Solution

by:
pepr earned 500 total points
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
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 …

772 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