Thread: class

Page 2 of 2 First 12
  • Jump to page:
    #16
  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
    Originally posted by netytan
    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:%d(seconds)' % locals()
    
    time = Time(6, 34, 30)
    Mark.
    netytan yes as an experiance c programmer i know that, that way is better and faster.. but i see that you have an extra % there at the end.. that is this for?
  2. #17
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Mmm i'm assuming your taling about the % at the very end between the string and the values you want inserting..

    'My mummy was %s' % 'a string formating char.. no really'

    In this case the % operator tells Python to expect values for formatting and then insert them into the the string, without this Python wont even try and inserting values leaving the string 'My mummy was %s'

    Note: if you supply values without the % you'll end up with a SyntaxError

    Mark.
    programming language development: www.netytan.com Hula

Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo