#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    Tampa, Florida
    Posts
    31
    Rep Power
    13

    Type casting in Python?


    I'm not even sure if type casting is how it's referred to in Python, but here goes.

    I am passing some arguments to a program (wmsetbg) and would like to pass a number denoting which desktop I want to be affected (wmsetbg sets the desktop background for Windowmaker). Something like "wmsetbg -w 6 image.jpg" will set the background only for desktop 6.

    That said, I tried this....

    Code:
    dtop = random.randrange(1, 7)
    out = command.getoutput('wmsetbg -w ' + dtop + ' ' + item)
    Now I should've known better, but PHP has seriously spoiled me. This results in a type error as dtop is an integer and I'm tyring to concat it in a string.

    So I'm curious how to convert dtop to a string with Python. I've spent the last two hours searching python.org and am not having any luck.

    Cheers,
    BDKR
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    Just use string formatting there:
    Code:
    dtop = random.randrange(1, 7)
    out = command.getoutput('wmsetbg -w %s %s' % (dtop, item))
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Yeah type-castings the right word or at least its what i call it. Anyway this is what you need and very easy in Python!

    string = str(), list = list(), tuple = tuple(), dictionary = dict()

    Note: these calls act differently depending on what type is passed in. Play around with them in the Python shell a little, i'm sure you'll figure out what does what

    And thats about it for the basic types, however there are others like unicode() if you need them .

    Mark.
    Last edited by netytan; February 11th, 2004 at 10:48 AM.
    programming language development: www.netytan.com Hula

  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    Tampa, Florida
    Posts
    31
    Rep Power
    13
    Thanx a million! I'm going to have to spend some time with the string formatting stuff in Python.

    As for ...

    string = str(), list = list(), tuple = tuple(), dictionary = dict()
    ...that makes all the sense in the world. In the script in question, I am allready using list().

    Thanx again,
    BDKR
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    Note: you generally don't actually use those constructor functions. String formatting is generally accepted as the most efficient way of interpolating variables of any type into strings.
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    Tampa, Florida
    Posts
    31
    Rep Power
    13
    Originally posted by Strike
    Note: you generally don't actually use those constructor functions. String formatting is generally accepted as the most efficient way of interpolating variables of any type into strings.
    OK. Message recieved. Besides, it looks familiar from C anyways.

    Cheers,
    BDKR

IMN logo majestic logo threadwatch logo seochat tools logo