473,788 Members | 2,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static Typing in Python

How do I force static typing in Python?

-Premshree Pillai

=====
-Premshree
[http://www.qiksearch.com/]

_______________ _______________ _______________ _______________ ____________
Yahoo! India Insurance Special: Be informed on the best policies, services, tools and more.
Go to: http://in.insurance.yahoo.com/licspecial/index.html

Jul 18 '05 #1
15 2676
"Premshree Pillai" <pr************ **@yahoo.co.in> wrote in message
news:ma******** *************** *************** @python.org...
How do I force static typing in Python?
By using another language. Try Java.

John Roth
-Premshree Pillai

=====
-Premshree


Jul 18 '05 #2
Premshree Pillai schrieb:
How do I force static typing in Python?


You have to enforce it by code instead of declaration, i.e. you
have to do runtime type checking. This could be done e.g. within
a class:

class doesTypeCheckin g:
def __init__self():
aString = ""
aFloat = 0.0

def __setAttr__(sel f, attr, value):
if type(self.__dic t__[attr] != type(value):
raise ValueError, "Type mismatch for attribute %s\n" % attr

Similar code could be used to check parameters of a function/
method.

Have looked at pychecker? It's not part of the python
distribution but it could help here.

Mit freundlichen Gruessen,

Peter Maas

--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
Tel +49-241-93878-0 Fax +49-241-93878-20 eMail pe********@mplu sr.de
-------------------------------------------------------------------
Jul 18 '05 #3
Peter Maas schrieb:
class doesTypeCheckin g:
def __init__self():


Correction:
def __init__(self):

--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
Tel +49-241-93878-0 Fax +49-241-93878-20 eMail pe********@mplu sr.de
-------------------------------------------------------------------
Jul 18 '05 #4
Peter Maas <fp********@net scape.net> writes:
Premshree Pillai schrieb:
How do I force static typing in Python?


You have to enforce it by code instead of declaration, i.e. you
have to do runtime type checking.


Just what do you understand "static typing" to mean ?
Jul 18 '05 #5
Peter Maas schrieb:
class doesTypeCheckin g:
def __init__(self):
aString = ""
aFloat = 0.0


sorry, I was too hasty. Here is the tested code:

class doesTypeCheckin g:
def __init__(self):
self.__dict__["aString"] = ""
self.__dict__["aFloat"] = 0.0

def __setattr__(sel f, attr, value):
if type(self.__dic t__[attr]) != type(value):
raise ValueError, "Type mismatch for attribute %s\n" % attr

Mit freundlichen Gruessen,

Peter Maas

--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
Tel +49-241-93878-0 Fax +49-241-93878-20 eMail pe********@mplu sr.de
-------------------------------------------------------------------
Jul 18 '05 #6
On 12 Mar 2004 13:56:53 +0100, Jacek Generowicz
<ja************ **@cern.ch> wrote:
Peter Maas <fp********@net scape.net> writes:
Premshree Pillai schrieb:
> How do I force static typing in Python?


You have to enforce it by code instead of declaration, i.e. you
have to do runtime type checking.


Just what do you understand "static typing" to mean ?

Doesn't that usually mean typing with a high noise-to-signal ratio?
Lots of punctuation and such.
;-)
--dang
Jul 18 '05 #7
Jacek Generowicz <ja************ **@cern.ch> writes:
Peter Maas <fp********@net scape.net> writes:
Premshree Pillai schrieb:
How do I force static typing in Python?


You have to enforce it by code instead of declaration, i.e. you
have to do runtime type checking.


Just what do you understand "static typing" to mean ?


I ask because I am prepared to accept that you have a different
working definition of "static typing" from mine, but people who
actually want static typing typically want it because they think it
gives them the following advantages:

a) type errors caught at compile time,

b) faster program exectution.

The code you showed:
class doesTypeCheckin g:
def __init__(self):
self.__dict__["aString"] = ""
self.__dict__["aFloat"] = 0.0

def __setattr__(sel f, attr, value):
if type(self.__dic t__[attr]) != type(value):
raise ValueError, "Type mismatch for attribute %s\n" % attr


will catch no type errors at compile time, and will slow down
execution, so I suspect that Premshree will be disapponinted with it.
(BTW, my definition of "static typing" is "type checking is done at
compile time" ... your example really looks like dynamic typing to
me ... albeit with some restrictions on attribute types.)
Premshree: Python is dynamically typed. There is no way to enforce
static typing. There is something called "pychecker" which
might be of some help to you. Why do you think that you
want static typing in Python ?
Jul 18 '05 #8
Dang Griffith <no*****@noemai l4u.com> writes:
On 12 Mar 2004 13:56:53 +0100, Jacek Generowicz
<ja************ **@cern.ch> wrote:

Just what do you understand "static typing" to mean ?

Doesn't that usually mean typing with a high noise-to-signal ratio?


What, you mean like this:

std::list<std:: pair<int, std::string> > l;
l.push_back(std ::pair<int, std::string>(1, "hello"));

as opposed to

l = [(1, 'hello')]

?

I like your definition :-)
Jul 18 '05 #9
On Sat, 13 Mar 2004 07:07:50 +0000 (GMT), Premshree Pillai
<pr************ **@yahoo.co.in> wrote:
--- Jacek Generowicz <ja************ **@cern.ch>
Yes, I am aware that Python is dynamically typed, and
so is Perl, right? In Perl, we have the "use strict
vars" pragma to force variable declaration. Is there
something like it in Python?
No, but you can use pychecker to get similar results.
Don't you think forced variable declaration is an
important requirement in a language?


Not really. Forced variable initialization is what's important.
Unlike C, et al, and Perl, variables don't have a default
initial value. If you try reference a variable that hasn't been
initialized ("bound to a value", in python lingo), python raises a
NameError exception.

--dang
p.s.
I know technically Perl initializes to 'undef', but it's magically
treated as 0 or an empty string, depending on context, so the
effect is much the same.
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2576
by: Michael Muller | last post by:
Is there currently any plan to introduce static typing in any future version of Python? (I'm not entirely sure that "static typing" is the right term: what I'm talking about is the declaration of types for variables, parameters and function return values). I know there was a "types SIG" which introduced several proposals, but it expired quite a while ago, and I was wondering whether whether some consensus has been reached on the subject...
19
1682
by: bearophile | last post by:
This is my first post here, I hope this is the right place to talk about such things. I have few comments and notes on the Python language. I've just started to learn it; this is negative because I'm ignorant still, but it's also positive because I can still see details that later probably I'll start to ignore. Some of the functions of IPython seem good and simple, and I think some of them can be integrated into the main Python line. ...
49
3125
by: bearophileHUGS | last post by:
Adding Optional Static Typing to Python looks like a quite complex thing, but useful too: http://www.artima.com/weblogs/viewpost.jsp?thread=85551 I have just a couple of notes: Boo (http://boo.codehaus.org/) is a different language, but I like its "as" instead of ":" and "->", to have: def min(a as iterable(T)) as T: Instead of:
6
1755
by: Christian Convey | last post by:
Hi guys, I'm looking at developing a somewhat complex system, and I think some static typing will help me keep limit my confusion. I.e.: http://www.artima.com/weblogs/viewpost.jsp?thread=87182 Does anyone know if/when that feature may become part of Python? Thanks very much,
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10364
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.