[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Is my Python script following correct idiomatic Python format?

Posted on 2013-11-13
1
Medium Priority
?
499 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 29

Accepted Solution

by:
pepr earned 2000 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

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.
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Six Sigma Control Plans
Suggested Courses

656 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