Thread: class

Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407

    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'
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    325
    Rep Power
    12
    yea you set it as

    time.minute = 34
    not
    time.minutes = 34
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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

  6. #4
  7. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    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
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    '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.
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    325
    Rep Power
    12
    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
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    For anyone intersted i found this snippet in O'really - Python in a Nutshell (great book)

    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.
    programming language development: www.netytan.com Hula

  16. #9
  17. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    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
  18. #10
  19. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    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( )
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    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.
  22. #12
  23. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    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 convert it to a better consept
  24. #13
  25. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Applying str() to a unicode string you say.. sorry i don't get this, i gave it a try and it worked without a problem

    >>> uni = u'Im a unicode string..really'
    >>> str(uni)
    'Im a unicode string..really'
    >>>

    Maybe somthing they fixed in Python 2.3? The problem with using ',' to concatenate strings (yes id still class this as concatenation) is that it throws a whitespace into the works but i havn't heard anything about the preformance here.. to be safe i'd probably guess the same thing applies to this operator..

    X: i find it helpful to think of methods with arguments as more like sub functions, i don't know if that helps or not though .. you could write this class better like this (assuming no other methods will need access to the hour, minutes, seconds)

    Code:
    class Time:
    
     def __init__(self, hours = 0, minutes = 0, seconds = 0):
      print '%d:%d:%d' % (hours, minutes, seconds)
    
    time = Time(6, 34, 30)
    ____________________
    
    class Time:
    
     def __init__(self, hours = 0, minutes = 0, seconds = 0):
      print '%(hours)d:%(minutes)d:%(seconds)d' % locals()
    
    time = Time(6, 34, 30)
    Mark.
    Last edited by netytan; November 11th, 2003 at 05:03 PM.
    programming language development: www.netytan.com Hula

  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    Netytan, try actually using some unicode data in the unicode string

    Your milage may vary, however. It depends on what system encoding you use.
  28. #15
  29. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    lol my bad ,

    A Unicode string using UTF-16 encoding (UTF-8 appears as normal chars) gives a UnicodeEncodeError (some chars can't be encoded using the ASCII codec) so you'll have to use the decode() method first..

    I'll have to do a little unicode work just too figure this out (havn't touched on it too deeply yet)

    Mark.
    Last edited by netytan; November 11th, 2003 at 10:21 AM.
    programming language development: www.netytan.com Hula

Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo