1

Im just try to build a basic class so that I can learn some more about python. So far I have the following :

class Bodymassindex:
 count = 0
 def __init__(self,name,weight,height):
 self.name = name
 self.weight = 14 * weight
 self.height = 12 * height
 notes = "no notes have been assigned yet"
 bmitotal = 0
 Bodymassindex.count += 1
 def displayCount(self):
 print "Total number of objects is %d" % Bodymassindex.count
 def notesBmi(self,text):
 self.notes = text
 def calcBmi(self):
 return ( self.weight * 703 ) / ( self.height ** 2 )

In terms of adding a note variable and viewing what is the correct way of doing so ?

Thanks,

asked Apr 26, 2012 at 12:56
8
  • 6
    It's worth noting PEP-8 suggests CapWords for class names and lowercase_with_underscores for function names. It's also worth noting that 'setters like notesBmi are not worth using in Python - make it an attribute and use it like one. If you need to do something there later, use property(). Commented Apr 26, 2012 at 13:02
  • 2
    When using python 2.x, it is often desireable to inherit from object ... e.g. class Bodymassindex(object): Commented Apr 26, 2012 at 13:08
  • 1
    @mgilson: often? It is always desirable. Commented Apr 26, 2012 at 13:09
  • 1
    @ChrisMorgan Yes -- Though for very basic scripts, you'll often not notice the difference and you can save a little typing by not putting it in there. But I agree. It is the best practice. Commented Apr 26, 2012 at 13:13
  • 1
    @ChrisMorgan not if you're building something which will be used as a mixin with a library that only provides old-style classes. That's rare, and to be avoided, but violates "always". Commented Apr 26, 2012 at 13:13

2 Answers 2

4

The bmitotal and notes variables in __init__ will be local and garbage collected when __init__ finishes, so initializing them like that is useless. You probably want to initialize them as self.notes and self.bmitotal

Bodymassindex.count would be like a static variable, which shares its value with all the instances.

answered Apr 26, 2012 at 13:04
Sign up to request clarification or add additional context in comments.

10 Comments

Also note that Bodymassindex.count can be reset on an instance...e.g. self.count = 5 which will not update the "static version".
yes, that's why i said *like* a static variable ;) i didn't want to confuse him too much.
@mgilson That's not true - it's not reset, rather, self.count will point to Bodymassindex.count unless there is an instance variable called count, in which case it points to that.
@mgilson The instance property and the class property are completely different objects. The only catch here is that when you call the instance property and it doesn't exist you get the class property object instead.
@mgilson It's as KurzedMetal states, saying the variable is reset implies that the variable is changing, it's nothing to do with that variable, what changes is what self.count means in the context of the program. If self.count doesn't exist as an instance variable, it means the static variable BodyMassIndex.count, otherwise it means the instance variable.
|
2

Just access the attribute:

class BodyMassIndex(object): #Inheriting from object in 2.x ensures a new-style class.
 count = 0
 def __init__(self, name, weight, height):
 self.name = name
 self.weight = 14 * weight
 self.height = 12 * height
 self.notes = None
 self.bmitotal = 0
 BodyMassIndex.count += 1
 def display_count(self):
 print "Total number of objects is %d" % BodyMassIndex.count
 def calculate_bmi(self):
 return ( self.weight * 703 ) / ( self.height ** 2 )
test = BodyMassIndex("bob", 10, 10)
test.notes = "some notes"
print(test.notes)

There is nothing wrong with direct access in Python. As others have noted, it's likely you meant to make notes and bmitotal instance variables, which I have done here.

answered Apr 26, 2012 at 13:05

2 Comments

It might be a better idea to set notes to None initially to make testing for the "no notes" case easier.
@larsmans This is good advice, I was simply sticking to what the OP did. Edited.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.