472,968 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,968 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 981
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

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

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
1
by: Erik Max Francis | last post by:
I've come across a limitation in unpickling certain types of complex data structures which involve instances that override __hash__, and was wondering if it was known (basic searches didn't seem to...
9
by: Aguilar, James | last post by:
I know that one can define an essentially unlimited number of classes in a file. And one can declare just as many in a header file. However, the question I have is, should I? Suppose that, to...
10
by: Joe Laughlin | last post by:
I'm sure there's a fairly easy answer for this... but how can I define a new type with range checking? Example: I want to define a new type that's like a double, except that you can only give...
6
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is...
5
by: Cmtk Software | last post by:
The following code: public __gc class MyClass { public: void MyFunc (int __gc* number); }; Generates the following metadata for C# when compiled in VC 2005 with the /clr:oldsyntax switch...
26
by: Cliff Williams | last post by:
Can someone explain the pros/cons of these different ways of creating a class? // 1 function myclass() { this.foo1 = function() {...} } // 2a
2
by: =?Utf-8?B?Z2FkeWE=?= | last post by:
I use one of 2 arrays dependent on the country. Rather than say: if exchangeID = 1 then dim myPlaceBets() as As UK.exchange.PlaceBets many statements myPlaceBetsReq.bets = myPlaceBets else...
11
by: whirlwindkevin | last post by:
I saw a program source code in which a variable is defined in a header file and that header file is included in 2 different C files.When i compile and link the files no error is being thrown.How is...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.