Python 强类型列表

发布于 2024-08-02 01:15:03 字数 539 浏览 9 评论 0原文

我正在使用 eclipse for python,但遇到了问题。 我有许多具有许多属性的类,并且想要来自我声明的类之一的对象列表。 问题是:当我访问列表中的任何项目时,IDE 不知道它的类型,因为在 python 中我们没有用类型声明变量,所以没有自动完成功能,我必须去类中复制属性名称。

为了使想法更清晰:

class AutomataBranch(object):
    def __init__(selfparams):
        self.Name="";
        self.nodes=[];

class LanguageAutomata(object):    
    def __init__(selfparams):
        self.cfgAutomata=[];#This has AutomaBranch Type

现在,在 LanguageAutomata 类的任何方法中,如果我写: cfgAutomata。 然后它不会给我 Name 属性 有什么解决办法吗?

I am using eclipse for python and I am facing a problem. I have many classes with many properties and want a list of objects from one of my declared classes. The problem is: When I am accessing any item from the list, the IDE does not know its type because in python we do not declare the variable with type, so there is no auto complete and I have to go to the class to copy the attribute name.

To make idea more clear:

class AutomataBranch(object):
    def __init__(selfparams):
        self.Name="";
        self.nodes=[];

class LanguageAutomata(object):    
    def __init__(selfparams):
        self.cfgAutomata=[];#This has AutomaBranch Type

Now in any method in LanguageAutomata class if I wrote:
cfgAutomata. Then it wont give me the Name attribute
Is there any solution for that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

踏月而来 2024-08-09 01:15:03

Python 是强类型的,Python 列表也是强类型的。 你的问题来自于Python是动态类型的。 因此 var 可以包含任何类型,因此 IDE 无法猜测参数的类型,也无法为您提供方法的代码完成。

就是这样,没有干净的解决方法。 如果这是一个问题,那么也许动态语言不是您偏爱的工具,您应该使用适合您的开发风格的语言。 每个人都有适合的工具。

Python is strongly typed and Python lists are too. Your problem come from the fact that Python is dynamically typed. Therefor a var can contain any type, and therefor no IDE can guess what is the type of your parameter, nor give you code completion for the methods.

This is how it is, there is no clean workaround. If it's a problem, then maybe dynamics language is not you predilection tool and you should use something that fit your development style. There are tools for everybody.

留蓝 2024-08-09 01:15:03

8 年后,我们实际上在 Python 3.6 中找到了解决方案。

PEP484 允许您主要为 IDE 和 linting 注释变量:

修改 @Hani 的答案:

x : AutomataBranch = self.cfgAutomata[i] 

现在任何优秀的 IDE 都会选择它来突出显示错误并允许自动完成。

8 years later and we actually have a solution in Python 3.6.

PEP484 allows you to annotate your variables primarily for IDEs and linting:

Modifying @Hani's answer:

x : AutomataBranch = self.cfgAutomata[i] 

This is now picked up by any good IDE to highlight errors and allow autocomplete.

把人绕傻吧 2024-08-09 01:15:03

我认为你的意思是说“静态类型”而不是“强类型”。 Python 是强类型的。 您只是在编译时不知道该类型是什么。

话虽如此,您确实需要放弃这样的想法:您将找到任何既适用于 Python 又适用于 Java 或 C# 的 IDE。 Python 的动态类型使这变得困难。 事实上,我倾向于发现功能强大的 IDE 更多的是一种负担而不是一种帮助。

I think you mean to say "statically typed" instead of "strongly typed." Python is strongly typed. You just don't know what that type is at compile time.

With that said, you really need to abandon the idea that you're going to find any IDEs that work as well for Python as they do for Java or C#. Python's dynamic typing makes this difficult. In fact, I tend to find that powerful IDEs are more of a burden than a help.

じ违心 2024-08-09 01:15:03

我想我找到了一个很好的易于管理的解决方案。 实际上它很微不足道,但可能会有所帮助(我现在使用它)。
当我想访问列表时,我将要访问的对象分配给变量 ex:

x = AutomataBranch() 
x = self.cfgAutomata[i] 

第一行仅用于让 IDE 知道 x 来自 AutomatBranch 类型。 之后,当我按 x 时,所有方法和属性都会可视化。

我想这是多么好的事情啊。

I think I found a good managable solution. Actually it is trivial but may help (I used it now).
When I want to access the list then I assign the object which I want to access to a variable ex:

x = AutomataBranch() 
x = self.cfgAutomata[i] 

The first line is used only to make the IDE knows that x is from AutomatBranch type. After that when I press x then all methods and properties are visualized.

I think it is some how good.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文