简体   繁体   English

如何在python项目中构造代码; 类或模块?

[英]How to structure code within a python project; classes or modules?

I am trying to write a primes module in python. 我正在尝试在python中编写一个primes模块。 One thing I would like to be able to write is 我想写的一件事是

>>> primes.primesLessThan(12)
[2, 3, 5, 7, 11]

However, I would also like to be able to write 但是,我也想写

>>> primes.primesLessThan.Sundaram(12)
[2, 3, 5, 7, 11]

to force it to use the Sieve of Sundaram. 迫使它使用圣达拉姆筛。 My original idea was to make primesLessThan a class with several static methods, but since __init__ can't return anything, this didn't let me achieve the first example. 我最初的想法是使用几个静态方法使primesLessThan类成为一个类,但是由于__init__无法返回任何内容,所以这并不能使我实现第一个示例。 Would this be better done as a seperate module that primes imports or is there something else I missed? 将其作为单独的模块启动进口是否更好,或者还有其他我想念的东西?

As a rule of thumb, if you have a class without any instance variables, an empty init method and just a bunch of static methods, then its probably going to be simpler to organize it as a module instead. 根据经验,如果您有一个没有任何实例变量的类,一个空的init方法和一堆静态方法,那么将其组织为模块可能会更简单。

#sieves module
def Sundaram(n):
  return [2,3,5,7]

def Eratosthenes(n):
  return [2,3,5,7]

And then you can use the functions from the module 然后您可以使用模块中的功能

import primes.sieves
primes.sieves.Sundaram(12)

Finally, python functions are first class and can be passed around in function parameter or stored in data structures. 最后,python函数是第一类,可以在函数参数中传递或存储在数据结构中。 This means that if you ever need to write some code that depends on an algorithm choice, you can just pass that as a parameter. 这意味着,如果您需要编写一些取决于算法选择的代码,则只需将其作为参数传递即可。

def test_first_primes(algorithm):
   return algorithm(10) == [2,3,5,7]

print (test_first_primes(Sundaram))
print (test_first_primes(Eratosthenes))

Two ways I can think of, to get these kinds of semantics. 我可以想到两种方式来获得这类语义。

  • Make primes a class, and then make primesLessThan a property. 使质数成为类,然后使质数少于属性。 It would also be a class, which implements __iter__ etc. to simulate a list, while also having some subfunctions. 它也将是一个类,它实现__iter__等以模拟列表,同时还具有一些子功能。 primesLessThan would be a constructor to that class, with the argument having a default to allow passing through. primesLessThan将是该类的构造函数,该参数具有默认值以允许传递。

  • Make primes itself support __getitem__ / __iter__ /etc. 使素数本身支持__getitem__ / __iter__ / __getitem__ You can still use properties (with default), but make primesLessThan just set some internal variable in the class, and then return self. 您仍然可以使用属性(默认情况下),但是使primesLessThan只需在类中设置一些内部变量,然后返回self。 This lets you do them in any order ie primes.Sundaram.primesLessThan(12) would work the same way as primes.primesLessThan.Sundaram(12), though, that looks strange to me. 这使您可以按任何顺序进行操作,即primes.Sundaram.primesLessThan(12)的工作方式与primes.primesLessThan.Sundaram(12)相同,但对我来说似乎很奇怪。

Either one of these are going to be a bit weird on the return values... you can create something that acts like a list, but it obviously won't be. 这些中的任何一个都会对返回值有些奇怪...您可以创建类似于列表的内容,但显然不会。 You can have repr show it like a list, and you'll be able to iterate over like a list (ie for prime in primes.Sundaram(12) ), but it can't return an actual list for obvious reasons.... 您可以让repr像列表一样显示它,并且可以像列表一样进行迭代(例如for prime in primes.Sundaram(12) ),但是由于明显的原因它不能返回实际的列表... 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导入和构建python模块/类 - import & structure python modules / classes 如何将Python代码构建到模块/包中? - How do I structure Python code into modules/packages? Python 项目结构 - 多个文件和模块 - Python Project Structure - multiple files and modules 如何重命名Django项目中的许多类和模块 - How to rename many classes and modules in a Django project 如何构建标准Python项目(模块,库)以供将来在Google App Engine中使用? - How to structure standard Python project (modules, libraries) for future use in Google App Engine? 如何构建我的Python项目以允许从子目录导入命名模块 - How do I structure my Python project to allow named modules to be imported from sub directories 如何从模块项目结构外部导入 python 模块而不出现 ModuleNotFoundError? - How can I import python modules from outside the module's project structure without getting a ModuleNotFoundError? 如何组织GAE模块的应用程序结构和代码? - How to organize GAE Modules app structure and code? 单个模块与多个模块的python 3项目结构 - python 3 project structure for single v.s. multiple modules 无法使用我当前的python项目结构加载模块 - Unable to load modules with my current python project structure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM