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 February 11th, 2004, 10:38 AM
BDKR's Avatar
BDKR BDKR is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Tampa, Florida
Posts: 31 BDKR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 1 m 13 sec
Reputation Power: 7
Send a message via AIM to BDKR Send a message via Yahoo to BDKR
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

Reply With Quote
  #2  
Old February 11th, 2004, 10:41 AM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
Just use string formatting there:
Code:
dtop = random.randrange(1, 7)
out = command.getoutput('wmsetbg -w %s %s' % (dtop, item))



Reply With Quote
  #3  
Old February 11th, 2004, 10:46 AM
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
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.
__________________
programming language development: www.netytan.com Hula


Last edited by netytan : February 11th, 2004 at 10:48 AM.

Reply With Quote
  #4  
Old February 11th, 2004, 01:20 PM
BDKR's Avatar
BDKR BDKR is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Tampa, Florida
Posts: 31 BDKR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 1 m 13 sec
Reputation Power: 7
Send a message via AIM to BDKR Send a message via Yahoo to BDKR
Thanx a million! I'm going to have to spend some time with the string formatting stuff in Python.

As for ...

Quote:
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

Reply With Quote
  #5  
Old February 11th, 2004, 02:32 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to 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.

Reply With Quote
  #6  
Old February 12th, 2004, 12:41 AM
BDKR's Avatar
BDKR BDKR is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Tampa, Florida
Posts: 31 BDKR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 1 m 13 sec
Reputation Power: 7
Send a message via AIM to BDKR Send a message via Yahoo to BDKR
Quote:
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Type casting in Python?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway