473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Creating a variable of a specific type

Hi people

I understand that Python is strongly, but also dynamically typed. However, I
need to create a variable which initially isn't actually set to anything,
but still needs to be of a certain type. I've looked around a bit, but I'm
not entirely sure how to do this.

Specifically, I have a bit of C++ code which says:

struct in_addr ipAddress;
struct sockaddr_in serverAddress;
map<int, socklen_t> clientAddressSizes;
fd_set primarySocketTable;
map<int, pthread_t> socketThreads;

....this is an except of a sample of different types that I am using. From
looking at the documentation for the Python socket module, for instance, I
can see that the function socket.inet_ntoa() requires a variable in struct
in_addr form, just like in C++. The difference is that I don't know how to
set up a 'blank' variable, if you see what I mean!

I'm sure it's just a simple thing that has eluded me somehow, but in
addition to answering this question if anyone can speak any wisdom about the
other data types I have listed above, and their equivalents in Python, that
would be helpful.

Cheers

Dan


Jul 18 '05 #1
5 1709

"Dan Williams" <da*@ithium.net> wrote in message
news:ma***************************************@pyt hon.org...
...this is an except of a sample of different types that I am using. From
looking at the documentation for the Python socket module, for instance, I
can see that the function socket.inet_ntoa() requires a variable in struct
in_addr form, just like in C++.


It would be more helpful to say that inet_ntoa is a way to deal with a
variable in packed format. Unless you are deserializing from a buffer
(e.g for a binary wire protocol), or embedding 'C' code you probably
don't need to use it anyway.

It might be worth investing a few minutes playing with the example
socket programs from the Python docs, even if you are an experienced
programmer: http://www.python.org/doc/current/li...t-example.html

Jul 18 '05 #2
> I understand that Python is strongly, but also dynamically typed. However,
I need to create a variable which initially isn't actually set to
anything, but still needs to be of a certain type. I've looked around a
bit, but I'm not entirely sure how to do this.

Specifically, I have a bit of C++ code which says:

struct in_addr ipAddress;
struct sockaddr_in serverAddress;
map<int, socklen_t> clientAddressSizes;
fd_set primarySocketTable;
map<int, pthread_t> socketThreads;

...this is an except of a sample of different types that I am using. From
looking at the documentation for the Python socket module, for instance, I
can see that the function socket.inet_ntoa() requires a variable in struct
in_addr form, just like in C++. The difference is that I don't know how to
set up a 'blank' variable, if you see what I mean!

I'm sure it's just a simple thing that has eluded me somehow, but in
addition to answering this question if anyone can speak any wisdom about
the other data types I have listed above, and their equivalents in Python,
that would be helpful.


While your are right that python is strong and dynamically typed, you have a
(very common) misconception about variables in python. In python, you have
values on the one side, and _names_ that are bound to certain values on the
other side. So

a = 10
a = "20"

is perfectly legal - and there is no way of limiting the types of values
bound to a (which is what you are asking for). Now while this might look
like weak typing, its not, as this example shows:

a = 10
b = "30"
a + b
TypeError: unsupported operand type(s) for +: 'i

That would go with perl or tcl - and even C! In c, the example would more
look like this:

int a = 10;
char *b = "30";
a + b;

This compiles without any complaints....

Now back to your problem: I don't know right from my head what socket
requires as inet-type, but I guess its a tuple of some sort. You don't need
to declare a variable for that - just use it. If you need to check for a
symbol not beeing initialized, simply use None as value, like here:

address = None
if address:
do_something(address)

None is considered false. You might be more explicit, by using

if address == None:

but thats a matter of taste (to me, at least...)
--
Regards,

Diez B. Roggisch
Jul 18 '05 #3
In article <bv*************@ID-111250.news.uni-berlin.de>,
Diez B. Roggisch <no**********@web.de> wrote:

Now back to your problem: I don't know right from my head what socket
requires as inet-type, but I guess its a tuple of some sort. You don't need
to declare a variable for that - just use it. If you need to check for a
symbol not beeing initialized, simply use None as value, like here:

address = None
if address:
do_something(address)

None is considered false. You might be more explicit, by using

if address == None:

but thats a matter of taste (to me, at least...)


No, that's not a matter of taste, it's a matter of incorrect coding.
Using ``==`` calls a method on address, which could return true even if
address isn't None. Much better to use ``is``, which is guaranteed to
return true only if address really *is* None.

Note that in the absence of special methods for comparison, all Python
objects are true, so your original formulation is especially appropriate.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"The joy of coding Python should be in seeing short, concise, readable
classes that express a lot of action in a small amount of clear code --
not in reams of trivial code that bores the reader to death." --GvR
Jul 18 '05 #4
On Thu, Feb 05, 2004 at 08:54:29AM -0500, Aahz wrote:
In article <bv*************@ID-111250.news.uni-berlin.de>,
Diez B. Roggisch <no**********@web.de> wrote:

Now back to your problem: I don't know right from my head what socket
requires as inet-type, but I guess its a tuple of some sort. You don't need
to declare a variable for that - just use it. If you need to check for a
symbol not beeing initialized, simply use None as value, like here:

address = None
if address:
do_something(address)

None is considered false. You might be more explicit, by using

if address == None:

but thats a matter of taste (to me, at least...)


No, that's not a matter of taste, it's a matter of incorrect coding.
Using ``==`` calls a method on address, which could return true even if
address isn't None. Much better to use ``is``, which is guaranteed to
return true only if address really *is* None.

Note that in the absence of special methods for comparison, all Python
objects are true, so your original formulation is especially appropriate.


Just as using "==" calls a method on address, which could return true even
if address isn't None, calling bool() with address may return false, even if
address isn't None! "if address:" may work in some cases, but it will
return incorrect results when address has been initialized to another false
value ([] is especially common, I find), when it is initalized to a class
defining __nonzero__/__len__ in certainly ways, and in some unfortunate
cases it may even raise an exception (eg, cgi.FieldStorage).

Jp

Jul 18 '05 #5

"Jp Calderone" <ex*****@intarweb.us> wrote in message
news:20********************@intarweb.us...
On Thu, Feb 05, 2004 at 08:54:29AM -0500, Aahz wrote:
In article <bv*************@ID-111250.news.uni-berlin.de>,
Diez B. Roggisch <no**********@web.de> wrote:

Now back to your problem: I don't know right from my head what socket
requires as inet-type, but I guess its a tuple of some sort. You don't needto declare a variable for that - just use it. If you need to check for asymbol not beeing initialized, simply use None as value, like here:

address = None
if address:
do_something(address)

None is considered false. You might be more explicit, by using

if address == None:

but thats a matter of taste (to me, at least...)
No, that's not a matter of taste, it's a matter of incorrect coding.
Using ``==`` calls a method on address, which could return true even if
address isn't None. Much better to use ``is``, which is guaranteed to
return true only if address really *is* None.

Note that in the absence of special methods for comparison, all Python
objects are true, so your original formulation is especially

appropriate.
Just as using "==" calls a method on address, which could return true even if address isn't None, calling bool() with address may return false, even if address isn't None! "if address:" may work in some cases, but it will
return incorrect results when address has been initialized to another false value ([] is especially common, I find), when it is initalized to a class
defining __nonzero__/__len__ in certainly ways, and in some unfortunate
cases it may even raise an exception (eg, cgi.FieldStorage).


Identity to None should be tested for with the 'is' operator (if address is
None:...), which quickly tests for identity of the two objects. Equality
of value and boolean value are both slower and a bit slippery (type
specific).

Terry J. Reedy


Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike Conmackie | last post by:
Greetings, I am trying to create a node in the output tree using a variable. Here are some fragments that I hope will explain the problem better. <xsl:stylesheet...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
8
by: Nanda | last post by:
hi, I am trying to generate parameters for the updatecommand at runtime. this.oleDbDeleteCommand1.CommandText=cmdtext; this.oleDbDeleteCommand1.Connection =this.oleDbConnection1;...
2
by: WRH | last post by:
Hello This is not a problem but rather curiosity on my part. If I have a Form, say Form1, with button controls and a variable, say public int x, then replicate it as per Form frm = new Form1()...
6
by: Raghu | last post by:
In C#, the typeof keyword can be used to get a type of the class. This does not require object to be created first. However O am not sure how do the same thing in vb. I don't want to create the...
6
by: Andrew Poulos | last post by:
If I code something like the following it results in a memory leak in IE (as Leak 0.5 tells me): var frm = document.createElement("FORM"); document.body.appendChild(frm); fDeleteForm =...
13
by: Justcallmedrago | last post by:
How would you declare and assign a variable inside a function THAT HAS THE NAME OF A PARAMETER YOU PASSED example: when you call createvariable("myvariable") it will declare the variable...
15
by: mark.norgate | last post by:
Hello I want to create a reference to an object, so that changes to the referenced object are reflected in the other object. Like this: object o = 123; object p = o; o = 456;
19
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post by:
I'm doing my c# more and more like i used to code c++, meaning i'm casting more often than creating an instance of objects. like : protected void gvOrderDetailsRowDataBound(object sender,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.