Python中的静态类型 [英] Static Typing in Python

查看:108
本文介绍了Python中的静态类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Python中强制静态输入?


-Premshree Pillai


=====

-Premshree

[ http://www.qiksearch.com /]


__________________________________________________ ______________________

Yahoo!印度保险特刊:了解最佳政策,服务,工具等。

请访问: http://in.insurance.yahoo.com/licspecial/index.html

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

推荐答案

" Premshree Pillai" < PR ************** @ yahoo.co.in>在留言中写道

新闻:ma ************************************ ** @ pyth on.org ...
"Premshree Pillai" <pr**************@yahoo.co.in> wrote in message
news:ma**************************************@pyth on.org...
如何在Python中强制静态输入?


使用其他语言。试试Java。


John Roth
-Premshree Pillai

=====
-Premshree
How do I force static typing in Python?
By using another language. Try Java.

John Roth
-Premshree Pillai

=====
-Premshree





Premshree Pillai schrieb:
Premshree Pillai schrieb:
如何在Python中强制静态输入?
How do I force static typing in Python?




你必须通过代码而不是声明强制执行它,即你必须进行运行时类型检查。这可以通过例如完成。

a类:


class doesTypeChecking:

def __init__self():

aString =" ;"

aFloat = 0.0


def __setAttr __(self,attr,value):

如果是type(self .__ dict__) [attr]!= type(value):

引发ValueError,类型不匹配属性%s \ n%attr


类似代码可以用于检查函数的参数/

方法。


看过pychecker?它不是python的一部分

分发,但它可以帮到这里。


Mit freundlichen Gruessen,


Peter Maas


-

-------------------------------------- -----------------------------

Peter Maas,M + R Infosysteme,D-52070 Aachen, Hubert-Wienen-Str.24

电话+ 49-241-93878-0传真+ 49-241-93878-20电子邮件 pe ******** @ mplusr.de

------------------- -------------------- ----------------------------



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 doesTypeChecking:
def __init__self():
aString = ""
aFloat = 0.0

def __setAttr__(self, attr, value):
if type(self.__dict__[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********@mplusr.de
-------------------------------------------------------------------


Peter Maas schrieb:
Peter Maas schrieb:
class doesTypeChecking:
def __init__self():
class doesTypeChecking:
def __init__self():




更正:

def __init __(self):


-

----------------------------- --------------------------------------

Peter Maas,M + R Infosysteme,D-52070 Aachen,Hubert-Wienen-Str。 24

电话+ 49-241-93878-0传真+ 49-241-93878-20电子邮件 pe********@mplusr.de

------------------------- ------------------------------------------



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********@mplusr.de
-------------------------------------------------------------------


这篇关于Python中的静态类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆