Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > utility functions within a class?

Reply
Thread Tools

utility functions within a class?

 
 
John Salerno
Guest
Posts: n/a
 
      05-07-2006
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!
 
Reply With Quote
 
 
 
 
blair.bethwaite@gmail.com
Guest
Posts: n/a
 
      05-08-2006
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, ...):
...

 
Reply With Quote
 
 
 
 
jeffshannon@gmail.com
Guest
Posts: n/a
 
      05-08-2006
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

 
Reply With Quote
 
blair.bethwaite@gmail.com
Guest
Posts: n/a
 
      05-08-2006
> 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!!

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

 
Reply With Quote
 
John Salerno
Guest
Posts: n/a
 
      05-08-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> 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.


Thanks, that makes sense to me! So basically just create them as
methods, and if I want a little 'privacy' I can lead with an underscore?
Somehow that sounds like the right idea...
 
Reply With Quote
 
John Salerno
Guest
Posts: n/a
 
      05-08-2006
(E-Mail Removed) wrote:

> Even if you don't end up referring to self or any instance
> attributes within the method


Hmm, follow-up: I *do* plan to refer to instance attributes inside these
methods (self.something), but does that require that they be instance
methods, or can they still reference 'self' since they are inside the class?

They won't be called directly from an instance, which is why I wondered
about making them static or class methods, but can static or class
methods use self, or does it have to be an instance method in that case?
 
Reply With Quote
 
John Salerno
Guest
Posts: n/a
 
      05-08-2006
John Salerno wrote:
> (E-Mail Removed) wrote:
>
>> Even if you don't end up referring to self or any instance
>> attributes within the method

>
> Hmm, follow-up: I *do* plan to refer to instance attributes inside these
> methods (self.something), but does that require that they be instance
> methods, or can they still reference 'self' since they are inside the class?
>
> They won't be called directly from an instance, which is why I wondered
> about making them static or class methods, but can static or class
> methods use self, or does it have to be an instance method in that case?


Ugh, sorry about another post, but let me clarify: In these utility
functions, I need to refer to an attribute of an instance, but these
functions will be called from another method in the class, not from the
instance itself. Is this even possible, or would 'self' have no meaning
when used this way?
 
Reply With Quote
 
blair.bethwaite@gmail.com
Guest
Posts: n/a
 
      05-08-2006
John Salerno wrote:
> Ugh, sorry about another post, but let me clarify: In these utility
> functions, I need to refer to an attribute of an instance, but these
> functions will be called from another method in the class, not from the
> instance itself. Is this even possible, or would 'self' have no meaning
> when used this way?


I'm having trouble deciphering what this bit means - "but these
functions will be called from another method in the class, not from the
instance itself", I don't think it makes sense.

If you're calling something in a class method then there must be an
instance of that class, instantiated as an object, in order to make the
call in the first place.

'self' does have meaning inside a class, you use it when you want to
call another method within the same class. Python can then fill in the
self reference in the arguments to the callee for you as it does when
you call a method from outside of the class by qualifying with the
instance name.
inside class A:
self.blah(args)
outside class A:
a_A = A()
a_A.blah(args)

Cheers,
-B
(Hope I've got this one right)

 
Reply With Quote
 
John Salerno
Guest
Posts: n/a
 
      05-08-2006
(E-Mail Removed) wrote:

> I'm having trouble deciphering what this bit means - "but these
> functions will be called from another method in the class, not from the
> instance itself", I don't think it makes sense.


Yeah, I'm starting to see that as I tried to implement it. Here's what I
came up with, which works:

def generate(self, filename):
self.head = self.generate_head()
self.body = self.generate_body()

So the two generate_* methods are called from within another class
method, and it seemed necessary to still call them from an instance.
What I originally meant was that they would not be called from an
instance *outside* the class itself, i.e. they won't be used when
writing another script, they are only used by the class itself.
 
Reply With Quote
 
blair.bethwaite@gmail.com
Guest
Posts: n/a
 
      05-08-2006
John Salerno wrote:
> What I originally meant was that they would not be called from an
> instance *outside* the class itself, i.e. they won't be used when
> writing another script, they are only used by the class itself.


Yep, so you want to encapsulate the functionality that those methods
provide, which is the whole point of building them in a class in the
first place. And you want them to be private to the class so that they
do not form part of the classes public/external interface.

In python, as discussed earlier , you can make them semi-private by
using the '__method_name' syntax. The thing is that python doesn't
strictly enforce this privacy, code outside of your object can still
call these methods because python just mangles the internal method name
slightly. See
http://docs.python.org/tut/node11.ht...00000000000000
for more detail.

Cheers,
-B

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using variadic functions within variadic functions pinkfloydhomer@gmail.com C Programming 2 02-27-2006 05:47 AM
accessing user defined utility functions Neil Zanella ASP .Net 5 01-23-2005 12:24 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
Generic iterator utility functions Carlo Milanesi C++ 0 11-15-2003 12:48 AM
Help with a utility class and having a method call another method from within Ian Python 2 09-06-2003 03:35 PM



Advertisments