|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
||||
|
||||
|
class
i was reading class methods section 14.2 printTime but.. when i tried the program.. i get an error
any helpCode:
class Time: def printTime(time): print str(time.hours) + ":" + str(time.minutes) + ":" + str(time.seconds) time = Time() time.hours = 6 time.minute = 34 time.seconds = 30 time.printTime( ) error: File "./4.py", line 11, in ? time.printTime( ) File "./4.py", line 5, in printTime print str(time.hours) + ":" + str(time.minutes) + ":" + str(time.seconds) AttributeError: Time instance has no attribute 'minutes'
__________________
IE QUOTE | PHP Manual | Google | C/C++ Compiler | Linux Tutorials | General Stuff Game Dev |
|
#2
|
|||
|
|||
|
yea you set it as
time.minute = 34 not time.minutes = 34 |
|
#3
|
||||
|
||||
|
Allow me to improve on the class you have there.. :S i'm a little comfused as to why they used such a poor example, not to mention one which defies prefered Python standards.
Code:
>>> class Time: def printTime(self): print '%d:%d:%d' % (self.hours, self.minutes, self.seconds) >>> time = Time() >>> time.hours = 6 >>> time.minutes = 34 >>> time.seconds = 30 >>> time.printTime() 6:34:30 >>> Note the use of 'self' instead of 'time', also note the string formatting over multiple str() calls and string concatenation. Granted i'm guilty of not using capitals in my own programs (unless required too). IMO they just make things complicated and for no good reason ![]() Mark. |
|
#4
|
||||
|
||||
|
rotf true, true, i do the same in php... anyways.. one thing.. what does self really means.. i mean i have a gasp understanding on it.. but not fully.. bro again thanx hehe
|
|
#5
|
||||
|
||||
|
'self' is used for inheritance perposes - you can use any other name you like but 'self' is the Python standard for this (like 'this' is in PHP and Java) - personally i think 'self' is a nice word for this but i surpose it comes down to personal choice
![]() If you want the actual word defintion of 'self' check out dictionary.com Mark. |
|
#6
|
|||
|
|||
|
Netytan, why would you use
print '%d:%d:%d' % (self.hours, self.minutes, self.seconds) instead of print str(time.hours) + ":" + str(time.minutes) + ":" + str(time.seconds) I personally prefer the high level way rahter then the 'C' way. Just wondering why you prefer the other? Cheers, Eli |
|
#7
|
||||
|
||||
|
Hey Eli,
There are actually a number of reasons to use string formatting over multiple string concatenation. I surpose the one your most interested in is the preformance; since strings in Python are imutable every time you concatenate a string with + your acutally creating a new copy of that string with the desired changes.. this isn't much a problem for most things but with larger strings it can be much slower! The second reason for using string formatting is because it saves on space which leads to smaller programs aswell as makes things easier to read and cleaner to look at! Eli: are you going to send me these classes to look at, i replied to your email ages ago and havn't head anything back from you.. Mark. |
|
#8
|
||||
|
||||
|
For anyone intersted i found this snippet in O'really - Python in a Nutshell (great book)
Quote:
Mark. |
|
#9
|
||||
|
||||
|
hehe here is another way that im going to bed for some help
Code:
class Time: def __init__(self,hours=0,minutes=0,seconds=0): print '%d:%d:%d' %(self.hours, self.minutes, self.seconds) time = Time(6,34,30) time.printTime( ) lets hope that you guys are not sick of me yet.. but inorder for me to understand some stuff.. i do have to post it someware .. or ask someone for a little help |
|
#10
|
||||
|
||||
|
ok nm i see now.. here is now i fixed it
Code:
class Time: def __init__(self,hours=0,minutes=0,seconds=0): self.hours = hours self.minutes = minutes self.seconds = seconds print '%d:%d:%d' %(self.hours, self.minutes, self.seconds) time = Time(6,34,30) #time.printTime( ) |
|
#11
|
|||
|
|||
|
There is another reason to use string formatting instead of string concatenation that does not apply to this example: If you try str() on a unicode string, it'll throw an exception, whereas string formatting will return a unicode string.
On the other hand, it's not that hard to write: Code:
print time.hour, ':', time.minutes, ':', time.seconds instead of using string concatenation or string formatting codes. |
|
#12
|
||||
|
||||
|
yes that is true.. i agree but when you frist starting of on a diffrent language using some one else example's then the right thing to do is runing there example to see what its doing.. and get a better understanding of the code.. then that way you can co |