By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,957 Members | 1,969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,957 IT Pros & Developers. It's quick & easy.

Creating a variable of a specific type

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a

"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

P: n/a
> 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

P: n/a
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

P: n/a
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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.