|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
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
|
||||
|
||||
|
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
|
|||
|
|||
|
Just use string formatting there:
Code:
dtop = random.randrange(1, 7)
out = command.getoutput('wmsetbg -w %s %s' % (dtop, item))
![]() |
|
#3
|
||||
|
||||
|
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. |
|
#4
|
||||
|
||||
|
Thanx a million! I'm going to have to spend some time with the string formatting stuff in Python.
As for ... Quote:
...that makes all the sense in the world. In the script in question, I am allready using list(). Thanx again, BDKR |
|
#5
|
|||
|
|||
|
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.
|
|
#6
|
||||
|
||||
|
Quote:
OK. Message recieved. Besides, it looks familiar from C anyways. Cheers, BDKR |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Type casting in Python? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|