Creating a variable of a specific type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dan Williams

    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> clientAddressSi zes;
    fd_set primarySocketTa ble;
    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_nto a() 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




  • Richard Brodie

    #2
    Re: Creating a variable of a specific type


    "Dan Williams" <dan@ithium.net > wrote in message
    news:mailman.12 36.1075978481.1 2720.python-list@python.org ...
    [color=blue]
    > ...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_nto a() requires a variable in struct
    > in_addr form, just like in C++.[/color]

    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





    Comment

    • Diez B. Roggisch

      #3
      Re: Creating a variable of a specific type

      > I understand that Python is strongly, but also dynamically typed. However,[color=blue]
      > 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> clientAddressSi zes;
      > fd_set primarySocketTa ble;
      > 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_nto a() 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.[/color]

      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(ad dress)

      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

      Comment

      • Aahz

        #4
        Re: Creating a variable of a specific type

        In article <bvtei9$10enm4$ 1@ID-111250.news.uni-berlin.de>,
        Diez B. Roggisch <nospam-deets@web.de> wrote:[color=blue]
        >
        >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(ad dress)
        >
        >None is considered false. You might be more explicit, by using
        >
        >if address == None:
        >
        >but thats a matter of taste (to me, at least...)[/color]

        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 (aahz@pythoncra ft.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

        Comment

        • Jp Calderone

          #5
          Re: Creating a variable of a specific type

          On Thu, Feb 05, 2004 at 08:54:29AM -0500, Aahz wrote:[color=blue]
          > In article <bvtei9$10enm4$ 1@ID-111250.news.uni-berlin.de>,
          > Diez B. Roggisch <nospam-deets@web.de> wrote:[color=green]
          > >
          > >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(ad dress)
          > >
          > >None is considered false. You might be more explicit, by using
          > >
          > >if address == None:
          > >
          > >but thats a matter of taste (to me, at least...)[/color]
          >
          > 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.[/color]

          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.FieldStorag e).

          Jp

          Comment

          • Terry Reedy

            #6
            Re: Creating a variable of a specific type


            "Jp Calderone" <exarkun@intarw eb.us> wrote in message
            news:2004020514 3140.GA22941@in tarweb.us...[color=blue]
            > On Thu, Feb 05, 2004 at 08:54:29AM -0500, Aahz wrote:[color=green]
            > > In article <bvtei9$10enm4$ 1@ID-111250.news.uni-berlin.de>,
            > > Diez B. Roggisch <nospam-deets@web.de> wrote:[color=darkred]
            > > >
            > > >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[/color][/color][/color]
            need[color=blue][color=green][color=darkred]
            > > >to declare a variable for that - just use it. If you need to check for[/color][/color][/color]
            a[color=blue][color=green][color=darkred]
            > > >symbol not beeing initialized, simply use None as value, like here:
            > > >
            > > >address = None
            > > >if address:
            > > > do_something(ad dress)
            > > >
            > > >None is considered false. You might be more explicit, by using
            > > >
            > > >if address == None:
            > > >
            > > >but thats a matter of taste (to me, at least...)[/color]
            > >
            > > 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[/color][/color]
            appropriate.[color=blue]
            >
            > Just as using "==" calls a method on address, which could return true[/color]
            even[color=blue]
            > if address isn't None, calling bool() with address may return false, even[/color]
            if[color=blue]
            > address isn't None! "if address:" may work in some cases, but it will
            > return incorrect results when address has been initialized to another[/color]
            false[color=blue]
            > 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.FieldStorag e).[/color]

            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




            Comment

            Working...