471,330 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

defining class related question(s)

Expand|Select|Wrap|Line Numbers
  1. class motion:
  2.           def  __init__(self, velocity, distance):
  3.                  self._v = velocity
  4.                  self._s = distance
  5.  
  6.           def  returnTime(self):
  7.                 return self._v/float(self._s)
  8.  
  9.  
  10.  
Now I would like to define a doubleTime = returnTime*2. I use the code below, it doesn't work
Expand|Select|Wrap|Line Numbers
  1. #
  2.           def doubleTime(self)
  3.                 return returnTime(self)*2
  4.  
How can I fix the second code
Oct 6 '07 #1
5 945
bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. class motion:
  2.           def  __init__(self, velocity, distance):
  3.                  self._v = velocity
  4.                  self._s = distance
  5.  
  6.           def  returnTime(self):
  7.                 return self._v/float(self._s)
  8.  
  9.  
  10.  
Now I would like to define a doubleTime = returnTime*2. I use the code below, it doesn't work
Expand|Select|Wrap|Line Numbers
  1. #
  2.           def doubleTime(self)
  3.                 return returnTime(self)*2
  4.  
How can I fix the second code
Expand|Select|Wrap|Line Numbers
  1. #
  2.           def doubleTime(self)
  3.                 return self.returnTime() * 2
  4.  
Python automatically fills in the first parameter (self) of class methods. It's probably the hardest thing to get used to when authoring classes.
Oct 6 '07 #2
It got an error
Expand|Select|Wrap|Line Numbers
  1. <bound method motion.doubleTime of <__main__.motion instance at 0x015C0698>
Oct 6 '07 #3
bartonc
6,596 Expert 4TB
It got an error
Expand|Select|Wrap|Line Numbers
  1. <bound method motion.doubleTime of <__main__.motion instance at 0x015C0698>
Here it is after testing:
Expand|Select|Wrap|Line Numbers
  1. class motion:
  2.     def  __init__(self, velocity, distance):
  3.         self._v = velocity
  4.         self._s = distance
  5.  
  6.     def  returnTime(self):
  7.         return self._v/float(self._s)
  8.  
  9.     # had a missing semi colon here #
  10.     def doubleTime(self):
  11.         return self.returnTime() * 2
  12.  
  13. m = motion(1, 2)
  14. print m.doubleTime()
Oct 6 '07 #4
Working now. However, when I tried another similar one it did not work.

Assuming that I already had my class (Cal) and everything related defined correctly

Expand|Select|Wrap|Line Numbers
  1. def t(self):
  2.     return self._Y//10
  3.  
  4. def v(self):
  5.     return self_.Y-self._t()*10.0
  6.  
  7. #if I used the cal.v(), it will return
  8. <bound method .....>>
  9.  
Oct 6 '07 #5
bartonc
6,596 Expert 4TB
Working now. However, when I tried another similar one it did not work.

Assuming that I already had my class (Cal) and everything related defined correctly

Expand|Select|Wrap|Line Numbers
  1. def t(self):
  2.     return self._Y//10
  3.  
  4. def v(self):
  5.     return self_.Y-self._t()*10.0
  6.  
  7. #if I used the cal.v(), it will return
  8. <bound method .....>>
  9.  
It's hard to tell from what you have posted. I can tell you that
self_.Y looks wrong and it is not self._Y.
Oct 7 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

50 posts views Thread by Dan Perl | last post: by
1 post views Thread by Erik Max Francis | last post: by
9 posts views Thread by Aguilar, James | last post: by
10 posts views Thread by Joe Laughlin | last post: by
6 posts views Thread by rodchar | last post: by
26 posts views Thread by Cliff Williams | last post: by
2 posts views Thread by =?Utf-8?B?Z2FkeWE=?= | last post: by
11 posts views Thread by whirlwindkevin | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.