一个类中的实用函数? [英] utility functions within a class?

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

问题描述

我可能会在这里遗漏一些明显的东西,但我决定通过编写一个涉及课程的程序来实验

,所以我有点新的

这个在Python中。


无论如何,在另一个函数(B)可以使用的类中创建函数(A)的最佳方法是什么?函数A不是一个

实例会调用的东西,所以我认为它是静态或

类方法之间的选择,但我不知道哪个一,或者如果这是正确的

方法。


具体来说,我正在编写一个定义包裹字符串的方法的类
$ b HTML元素中的$ b参数,最终创建和HTML页面。我知道可能已经有很多像这样的程序了,但这只是为了给我一些与Python有关的东西。


所以我有一个generate()方法,它将创建最终的HTML文件

你完成创建元素。首先它会创建一个带有

正确DTD的字符串,然后它会附加< head>元素和< body>

元素,包含在< html>中元素。


而不是让generate()函数完成所有工作,我以为我可以写两个实用函数来生成头部和身体
br />
元素。这些只是将元素名称包裹在一些

预定义文本(对于头部)和所有其他元素周围(对于

正文)。


所以我想知道,我将如何定义这两个函数?他们将从生成方法中调用

,仅用于创建

头部和身体块。


谢谢!

I might be missing something obvious here, but I decided to experiment
with writing a program that involves a class, so I''m somewhat new to
this in Python.

Anyway, what is the best way to create a function (A) within a class
that another function (B) can use? Function A is not something that an
instance will ever call, so I figure it''s a choice between static or
class methods, but I don''t know which one, or if this is even the right
approach.

Specifically, I am writing a class that defines methods that wrap string
arguments inside HTML elements, and ultimately creates and HTML page. I
know there are probably a ton of programs like this already, but this is
just to give me something to do with Python.

So I have a generate() method that will create the final HTML file once
you are done creating elements. First it will create a string with the
proper DTD, then it will append the <head> element and the <body>
element, wrapped in the <html> element.

Rather than have the generate() function do all the work, I thought I
could write two utility functions to generate the head and body
elements. These would simply wrap the element names around some
pre-defined text (for the head) and around all the other elements (for
the body).

So I''m wondering, how would I define these two functions? They would be
called from the generate method solely for the purpose of creating the
head and body blocks.

Thanks!

推荐答案

听起来你想要的只是一些封装,以下是

方法__head__和__body__" private" - 双重下划线重要的是
。我建议阅读python教程的对象位




类HTMLWrapper:


def generate(self,...):

...

self .__ head __(foo)

...

self .__ body __(bar)

...


def __head __(self,...):

...


def __body __(自我,......):

...

It sounds like all you want is some encapsulation, the following makes
methods __head__ and __body__ "private" - the double underscores are
important. I''d suggest reading the Object bits of the python tutorial
also.

class HTMLWrapper:

def generate(self, ...):
...
self.__head__(foo)
...
self.__body__(bar)
...

def __head__(self, ...):
...

def __body__(self, ...):
...


你做*不*想在方法之前和之后加上双下划线

name。这并不表示私人方法,它表示魔法

方法。 - 对Python有特殊意义的东西。因此,你

有特殊方法,如__init __(),__ len __(),__ getattr __(),

__setattr __()等;所有这些方法都可以通过Python *在某些特定情况下调用* b $ b,并允许您自定义

对象如何响应这些情况。


命名方法*不是*特殊的魔术方法使用这个命名

约定是一个非常糟糕的主意。


另一方面,方法只有一个或两个*领先*

下划线,并且没有尾随下划线,可以被视为表达某种程度的隐私。按照惯例,单个下划线表示内部方法或属性,并且该名称通常不会导出
。双下划线将触发最小名称修改;不是

只是名称不会被导出,但它会被转换为

_< classname> __< name>,使得访问起来有点困难

课外。 (在课堂内访问是通过unmangled

名称。)


在这种情况下,我可能有方法_generate_head(self)

和_generate_body(self)。不要搞砸他们上课/静态

方法,除非在你没有能够访问它们时很重要
有任何实例可用的课程(如果你在常规方法中调用它们

,那么你* *有一个实例

可用)。即使你最终没有在方法中引用self或任何实例

属性,将它保持为正常的

方法也更简单。 br />

- 杰夫香农

You do *NOT* want to put double-underscores before and after a method
name. That does not indicate a private method, it indicates a "magic
method" -- something that has special meaning to Python. Thus, you
have special methods like __init__(), __len__(), __getattr__(),
__setattr__(), etc; all of these methods may be called *by Python* in
certain specific circumstances, and allow you to customize how your
objects respond to those circumstances.

Naming methods that are *not* special "magic methods" using this naming
convention is a very bad idea.

On the other hand, methods with only a single or double *leading*
underscore, and no trailing underscore(s), can be considered to express
some degree of privacy. A single underscore, by convention, indicates
an internal method or attribute, and that name will typically not be
exported. A double underscore will trigger minimal name mangling; not
only will the name not be exported, but it will be converted to
_<classname>__<name>, making it a bit more difficult to access from
outside the class. (Access inside the class is via the unmangled
name.)

In this circumstance, I''d probably have methods _generate_head(self)
and _generate_body(self). Don''t mess with making them class/static
methods unless it''s important to be able to access them when you don''t
have any instance of the class available (and if you''re calling them
from inside a regular method, then you *do* have an instance
available). Even if you don''t end up referring to self or any instance
attributes within the method, it''s simpler to keep it as a normal
method.

--Jeff Shannon


>你不想*在方法
> You do *NOT* want to put double-underscores before and after a method
名称之前和之后放置双下划线。这并不表示私有方法,它表示魔法
方法
name. That does not indicate a private method, it indicates a "magic
method"




WHOOPS !!


抱歉,我几个月没有触及python,刚开始

今天早上写了一个脚本,所以我发布了自己的问题

当我得到的时候回答这个问题。我想这可能是一个很好的理由不使用下划线作为重要的语法。


干杯,

-B



WHOOPS!!

Sorry, I haven''t touched python for a few months and just started
working on a script this morning so was going to post my own question
when I got sidetracked answering this thread. I guess that might be a
good reason not to use underscores as significant syntax.

Cheers,
-B


这篇关于一个类中的实用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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