Python type checking?

Justin Sheehy dworkin at ccs.neu.edu
Tue Dec 7 16:00:09 EST 1999


Arinte <jamarijr at hotmail.com> writes:

>    def setValue(self, value):
>       self.argvalue = value
> 
> On the set setValue(), they set it to an integer value I want to make
> it a long else if it is a string leave it as a string.  

Your description is a little ambiguous, since you don't say what to do 
if `value' is neither an int or a string.

The simplest thing would be:

def setValue(self, value): 
    try:
        self.argvalue = long(value)
    except:
        self.argvalue = value

But if you wanted something to do exactly what you described, it could
look a little more like:

def setValue(self, value):
    if type(value) == type(0):
       self.argvalue = long(value)
    elif type(value) == type(''):
         self.argvalue = value 
    else:
        whatever you want to do if value is neither an int or a string

-Justin

 

From: Aaron J Reichow <reic0024 at bulldog5.d.umn.edu>
Newsgroups: comp.lang.python
Subject: Re: Is there a database in Zope?
Date: Tue, 7 Dec 1999 14:57:43 -0600
Organization: University of Minnesota, Duluth
Lines: 14
Message-ID: <Pine.SO4.4.02.9912071450120.13492-100000 at bulldog5.d.umn.edu>
References: <Or_24.723$tt.57908 at news.uswest.net> <82iv6c$fis$1 at newshost.accu.uu.nl>
NNTP-Posting-Host: bulldog5.d.umn.edu
Mime-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
To: Martijn Faassen <m.faassen at vet.uu.nl>
In-Reply-To: <82iv6c$fis$1 at newshost.accu.uu.nl>
Path: news!uunet!ffx.uu.net!finch!logbridge.uoregon.edu!newshub.tc.umn.edu!hardy.tc.umn.edu!news.d.umn.edu!bulldog5.d.umn.edu!reic0024
Xref: news comp.lang.python:77839
Sender: python-list-admin at python.org
Errors-To: python-list-admin at python.org
X-BeenThere: python-list at python.org
X-Mailman-Version: 1.2 (experimental)
Precedence: bulk
List-Id: General discussion list for the Python programming language <python-list.python.org>

> What kind of OS are you running? Getting ODBC to work on NT or Postgres on
> Linux isn't that hard. Alternatively Zope comes with Gadfly, an SQL database
> implemented in Python.

I second the mention of gadfly. I found it quite easy to set it up for a
simple name/address/phone/IM/phone/email/add'l info DB for any contacts
and friends.  About 15 lines of CGI in two different python CGI
scripts for users to view and add entries.  Very little time to
set it up.  Deleting and modifying entries will be a little harder, but
not excrutiatingly so.

Aaron




More information about the Python-list mailing list