简体   繁体   English

我应该在其中使用类吗:使用lxml读取XML文件

[英]Should I use a class in this: Reading a XML file using lxml

This question is in continuation to my previous question, in which I asked about passing around an ElementTree. 这个问题是我之前的问题的延续,在之前的问题中,我询问了如何传递ElementTree。

I need to read the XML files only and to solve this, I decided to create a global ElementTree and then parse it wherever required. 我只需要读取XML文件并解决此问题,我决定创建一个全局ElementTree,然后在需要时对其进行解析。

My question is: 我的问题是:

Is this an acceptable practice? 这是可以接受的做法吗? I heard global variables are bad. 我听说global变量不好。 If I don't make it global , I was suggested to make a class . 如果我不global ,建议我上一class But do I really need to create a class ? 但是我真的需要创建一个class吗? What benefits would I have from that approach. 我将从这种方法中获得什么好处。 Note that I would be handling only one ElementTree instance per run, the operations are read-only. 请注意,我每次运行只能处理一个 ElementTree实例,这些操作是只读的。 If I don't use a class , how and where do I declare that ElementTree so that it available globally? 如果我不使用class ,那么如何在哪里声明ElementTree以便全局可用? (Note that I would be importing this module) (请注意,我将导入此模块)

Please answer this question in the respect that I am a beginner to development, and at this stage I can't figure out whether to use a class or just go with the functional style programming approach. 请就我是开发的初学者而言回答这个问题,在此阶段我无法弄清楚是使用类还是仅使用函数样式编程方法。

Pragmatically, is your code expected to grow? 实用上,您的代码是否有望增长? Even though people herald OOP as the right way, I found that sometimes it's better to weigh cost:benefit(s) whenever you refactor a piece of code. 尽管人们认为OOP是正确的方法,但我发现有时候在重构代码时,权衡cost:benefit还是更好。 If you are looking to grow this, then OOP is a better option in that you can extend and customise any future use case, while saving yourself from unnecessary time wasted in code maintenance. 如果您希望扩展它,那么OOP是一个更好的选择,因为您可以扩展和自定义任何将来的用例,同时避免不必要的代码维护时间。 Otherwise, if it ain't broken, don't fix it, IMHO. 否则,如果它没有损坏,请不要修复它,恕我直言。

I generally find myself regretting it when I give in to the temptation to give a module, for example, a load_file() method that sets a global that the module's other functions can then use to find the file they're supposed to be talking about. 当我屈服于提供模块的诱惑时,我通常会后悔,例如, load_file()方法设置了一个全局值,该模块的其他函数可以随后使用该全局函数找到他们应该谈论的文件。 It makes testing far more difficult, for example, and as soon as I need two XML files there is a problem. 例如,它使测试变得更加困难,并且当我需要两个XML文件时,就出现了问题。 Plus, every single function needs to check whether the file's there and give an error if it's not. 另外,每个函数都需要检查文件是否存在,如果没有,则给出错误。

If I want to be functional, I simply therefore have every function take the XML file as an argument. 如果我想发挥作用,那么我只是让每个函数都将XML文件作为参数。

If I want to be object oriented, I'll have a MyXMLFile class whose methods can just look at self.xmlfile or whatever. 如果我想面向对象,我将有一个MyXMLFile类,该类的方法可以查看self.xmlfile或其他内容。

The two approaches are more or less equivalent when there's just one single thing, like a file, to be passed around; 当只有一个东西(例如文件)要传递时,这两种方法或多或少是等效的。 but when the number of things in the "state" becomes larger than a few, then I find classes simpler because I can stick all of those things in the class. 但是,当“状态”中的事物数量大于几个时,我发现类更简单,因为我可以将所有这些事物粘贴到类中。

(Am I answering your question? I'm still a big vague on what kind of answer you want.) (我在回答您的问题吗?对于您想要哪种答案,我还是个很大的疑问。)

There are a few reasons that global variables are bad . 全局变量不好的原因有很多。 First, it gets you in the habit of declaring global variables which is not good practice, though in some cases globals make sense -- PI, for instance. 首先,它使您养成声明全局变量的习惯,这不是一个好习惯,尽管在某些情况下,全局变量是有意义的,例如PI。 Globals also create problems when you on purpose or accidentally re-use the name locally. 当您故意或在本地意外使用名称时,全局变量也会造成问题。 Or worse, when you think you're using the name locally but in reality you're assigning a new value to the global variable. 或更糟糕的是,当您认为自己在本地使用名称时,实际上却是为全局变量分配了一个新值。 This particular problem is language dependent, and python handles it differently in different cases. 这个特定的问题取决于语言,并且python在不同情况下的处理方式有所不同。

class A:
   def __init__(self):
      self.name = 'hi'

x = 3
a = A()

def foo():
   a.name = 'Bedevere'
   x = 9

foo()
print x, a.name #outputs 3 Bedevere

The benefit of creating a class and passing your class around is you will get a defined, constant behavior, especially since you should be calling class methods, which operate on the class itself. 创建类并传递类的好处是,您将获得定义的,恒定的行为,特别是因为您应该调用对类本身进行操作的类方法。

class Knights:
   def __init__(self, name='Bedevere'):
       self.name = name
   def knight(self):
       self.name = 'Sir ' + self.name
   def speak(self):
       print self.name + ":", "Run away!"

class FerociousRabbit:
   def __init__(self):
       self.death = "awaits you with sharp pointy teeth!"
   def speak(self):
       print "Squeeeeeeee!"

def cave(thing):
   thing.speak()
   if isinstance(thing, Knights):
       thing.knight()

def scene():
   k = Knights()
   k2 = Knights('Launcelot')
   b = FerociousRabbit()
   for i in (b, k, k2):
      cave(i)

This example illustrates a few good principles. 这个例子说明了一些好的原则。 First, the strength of python when calling functions - FerociousRabbit and Knights are two different classes but they have the same function speak(). 首先,Python在调用函数时的优势-FerociousRabbit和Knights是两个不同的类,但是它们具有相同的函数talk()。 In other languages, in order to do something like this, they would at least have to have the same base class. 在其他语言中,为了执行此类操作,它们至少必须具有相同的基类。 The reason you would want to do this is it allows you to write a function (cave) that can operate on any class that has a 'speak()' method. 您要执行此操作的原因是它允许您编写一个函数(凹面),该函数可以在具有“ speak()”方法的任何类上运行。 You could create any other method and pass it to the cave function: 您可以创建任何其他方法并将其传递给Cave函数:

class Tim:
   def speak(self):
       print "Death awaits you with sharp pointy teeth!"

So in your case, when dealing with an elementTree, say sometime down the road you need to also start parsing an apache log. 因此,在您的情况下,当处理elementTree时,说一些话,您还需要开始解析apache日志。 Well if you're doing purely functional program you're basically hosed. 好吧,如果您正在执行纯功能性程序,则基本上是软管。 You can modify and extend your current program, but if you wrote your functions well, you could just add a new class to the mix and (technically) everything will be peachy keen. 您可以修改和扩展您的当前程序,但是如果您编写的函数很好,则可以向组合中添加一个新类,并且(从技术上来说)所有内容都非常热衷。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM