Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old November 10th, 2003, 04:49 PM
xlordt's Avatar
xlordt xlordt is offline
Only the strong survives!!.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Feb 2003
Location: A World of wonder.
Posts: 5,520 xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 4 Weeks 1 Day 16 h 36 m 39 sec
Reputation Power: 378
Send a message via ICQ to xlordt Send a message via AIM to xlordt Send a message via MSN to xlordt Send a message via Yahoo to xlordt Send a message via Google Talk to xlordt Send a message via Skype to xlordt
Facebook
class

i was reading class methods section 14.2 printTime but.. when i tried the program.. i get an error any help


Code:
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'

Reply With Quote
  #2  
Old November 10th, 2003, 05:26 PM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 6
yea you set it as

time.minute = 34
not
time.minutes = 34

Reply With Quote
  #3  
Old November 10th, 2003, 06:03 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 18 m 50 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #4  
Old November 10th, 2003, 06:11 PM
xlordt's Avatar
xlordt xlordt is offline
Only the strong survives!!.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Feb 2003
Location: A World of wonder.
Posts: 5,520 xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 4 Weeks 1 Day 16 h 36 m 39 sec
Reputation Power: 378
Send a message via ICQ to xlordt Send a message via AIM to xlordt Send a message via MSN to xlordt Send a message via Yahoo to xlordt Send a message via Google Talk to xlordt Send a message via Skype to xlordt
Facebook
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

Reply With Quote
  #5  
Old November 10th, 2003, 06:24 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 18 m 50 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
'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.

Reply With Quote
  #6  
Old November 10th, 2003, 06:28 PM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 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

Reply With Quote
  #7  
Old November 10th, 2003, 06:42 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 18 m 50 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #8  
Old November 10th, 2003, 06:52 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 18 m 50 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
For anyone intersted i found this snippet in O'really - Python in a Nutshell (great book)

Quote:
O'really - Python in a Nutshell
oneway = 'x' + str(j) + 'y' + str(j) + 'z'
another = 'x%sy%sz' % (j, j)
After this code is executed, variables oneway and another will always be equal, but the computation of another, done via string formatting, is measurably faster. Which way is clearer and simpler is a matter of habit: get used to the string-formatting idiom, and it will come to look simpler and clearer.


Mark.

Reply With Quote
  #9  
Old November 10th, 2003, 09:25 PM
xlordt's Avatar
xlordt xlordt is offline
Only the strong survives!!.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Feb 2003
Location: A World of wonder.
Posts: 5,520 xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 4 Weeks 1 Day 16 h 36 m 39 sec
Reputation Power: 378
Send a message via ICQ to xlordt Send a message via AIM to xlordt Send a message via MSN to xlordt Send a message via Yahoo to xlordt Send a message via Google Talk to xlordt Send a message via Skype to xlordt
Facebook
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

Reply With Quote
  #10  
Old November 10th, 2003, 09:35 PM
xlordt's Avatar
xlordt xlordt is offline
Only the strong survives!!.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Feb 2003
Location: A World of wonder.
Posts: 5,520 xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 4 Weeks 1 Day 16 h 36 m 39 sec
Reputation Power: 378
Send a message via ICQ to xlordt Send a message via AIM to xlordt Send a message via MSN to xlordt Send a message via Yahoo to xlordt Send a message via Google Talk to xlordt Send a message via Skype to xlordt
Facebook
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( )

Reply With Quote
  #11  
Old November 10th, 2003, 11:11 PM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
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.

Reply With Quote
  #12  
Old November 11th, 2003, 12:17 AM
xlordt's Avatar
xlordt xlordt is offline
Only the strong survives!!.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Feb 2003
Location: A World of wonder.
Posts: 5,520 xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108181 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 4 Weeks 1 Day 16 h 36 m 39 sec
Reputation Power: 378
Send a message via ICQ to xlordt Send a message via AIM to xlordt Send a message via MSN to xlordt Send a message via Yahoo to xlordt Send a message via Google Talk to xlordt Send a message via Skype to xlordt
Facebook
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