472,986 Members | 3,031 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 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 1687

"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,...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.