473,414 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 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 987
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.