Creating a variable of a specific type

D

Dan Williams

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
 
R

Richard Brodie

...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/lib/socket-example.html
 
D

Diez B. Roggisch

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...)
 
A

Aahz

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 ([email protected]) <*> 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
 
J

Jp Calderone

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
 
T

Terry Reedy

Jp Calderone said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top