When is a class more useful to use than a function? Is there any hard or fast rule that I should know about? Is it language dependent? I'm intending on writing a script for Python which will parse different types of json data, and my gut feeling is that I should use a class to do this, versus a function.
何时使用的函数比函数更有用?我应该知道有什么硬性或快速的规则吗?它是语言依赖的吗?我打算为Python编写一个脚本来解析不同类型的json数据,我的直觉是我应该使用一个类来执行此操作,而不是函数。
5 个解决方案
#1
8
You should use a class when your routine needs to save state. Otherwise a function will suffice.
当你的例程需要保存状态时,你应该使用一个类。否则一个功能就足够了。
#2
3
First of all, I think that isn't language-dependent (if the language permit you to define classes and function as well).
首先,我认为这不依赖于语言(如果语言允许您定义类和函数)。
As a general rule I can tell you that a Class wrap into itself a behaviour. So, if you have a certain type of service that you have to implement (with, i.e. different functions) a class is what you're lookin' for.
Moreover classes (say object that is more correct) has state and you can instantiate more occurrences of a class (so different objects with different states).
作为一般规则,我可以告诉你,一个类将自身包裹成一种行为。因此,如果您必须实现某种类型的服务(具有不同的功能),那么您正在寻找一个类。此外,类(比如更正确的对象)具有状态,您可以实例化更多类的出现(因此具有不同状态的不同对象)。
Not less important, a class can be inearthed: so you can overwrite a specific behaviour of your function only with small changes.
同样重要的是,一个类可以被挖掘出来:所以你只能通过很小的改动来覆盖你的函数的特定行为。
#3
1
the class when you have the state - something that should be persistent across the calls
当你拥有状态时的类 - 在调用中应该持久的东西
the function in other cases
其他情况下的功能
exception: if your class is only storing couple of values and has a single method besides __init__, you should better use the function
例外:如果你的类只存储了几个值并且除了__init__之外还有一个方法,你应该更好地使用该函数
#4
1
For anything non-trivial, you should probably be using a class. I tend to limit all of my "free-floating" functions to a utils.py file.
对于任何非平凡的事情,你应该使用一个类。我倾向于将所有“*浮动”函数限制为utils.py文件。
#5
1
This is language-dependent.
这取决于语言。
Some languages, like Java, insist that you use a class for everything. There's simply no concept of a standalone function.
有些语言,比如Java,坚持要为所有东西使用类。根本没有独立功能的概念。
Python isn't like that. It's perfectly OK - in fact recommended - to define functions standalone, and related functions can be grouped together in modules. As others have stated, the only time you really want a class in Python is when you have state that you need to keep - ie, encapsulating the data within the object.
Python不是那样的。完全可以 - 事实上建议 - 单独定义功能,相关功能可以组合在一起。正如其他人所说的那样,你真正想要Python中的一个类的唯一时间就是你需要保持状态 - 即将数据封装在对象中。
#1
8
You should use a class when your routine needs to save state. Otherwise a function will suffice.
当你的例程需要保存状态时,你应该使用一个类。否则一个功能就足够了。
#2
3
First of all, I think that isn't language-dependent (if the language permit you to define classes and function as well).
首先,我认为这不依赖于语言(如果语言允许您定义类和函数)。
As a general rule I can tell you that a Class wrap into itself a behaviour. So, if you have a certain type of service that you have to implement (with, i.e. different functions) a class is what you're lookin' for.
Moreover classes (say object that is more correct) has state and you can instantiate more occurrences of a class (so different objects with different states).
作为一般规则,我可以告诉你,一个类将自身包裹成一种行为。因此,如果您必须实现某种类型的服务(具有不同的功能),那么您正在寻找一个类。此外,类(比如更正确的对象)具有状态,您可以实例化更多类的出现(因此具有不同状态的不同对象)。
Not less important, a class can be inearthed: so you can overwrite a specific behaviour of your function only with small changes.
同样重要的是,一个类可以被挖掘出来:所以你只能通过很小的改动来覆盖你的函数的特定行为。
#3
1
the class when you have the state - something that should be persistent across the calls
当你拥有状态时的类 - 在调用中应该持久的东西
the function in other cases
其他情况下的功能
exception: if your class is only storing couple of values and has a single method besides __init__, you should better use the function
例外:如果你的类只存储了几个值并且除了__init__之外还有一个方法,你应该更好地使用该函数
#4
1
For anything non-trivial, you should probably be using a class. I tend to limit all of my "free-floating" functions to a utils.py file.
对于任何非平凡的事情,你应该使用一个类。我倾向于将所有“*浮动”函数限制为utils.py文件。
#5
1
This is language-dependent.
这取决于语言。
Some languages, like Java, insist that you use a class for everything. There's simply no concept of a standalone function.
有些语言,比如Java,坚持要为所有东西使用类。根本没有独立功能的概念。
Python isn't like that. It's perfectly OK - in fact recommended - to define functions standalone, and related functions can be grouped together in modules. As others have stated, the only time you really want a class in Python is when you have state that you need to keep - ie, encapsulating the data within the object.
Python不是那样的。完全可以 - 事实上建议 - 单独定义功能,相关功能可以组合在一起。正如其他人所说的那样,你真正想要Python中的一个类的唯一时间就是你需要保持状态 - 即将数据封装在对象中。