please comment - my first classes

Hilbert hilbert at microsoft.com
Fri Mar 14 11:38:35 EST 2003


Thanks for you advice, Sean.
I'll change the printing method
to return a string rather.

Hilbert

"Sean 'Shaleh' Perry" <shalehperry at attbi.com> wrote in message
news:mailman.1047625883.5580.python-list at python.org...
On Thursday 13 March 2003 22:53, Hilbert wrote:
> Hello,
>
> I started writing a script to keep track of my
> bookmarks.
> This is the first script where I'm using classes.
> I wanted to ask your opinions so that I know
> I'm on the right (or wrong) path
> I want to turn this into a cgi script, so the input
> part is just for testing. I'm not sure if this is the
> correct way for setting up the groups class.
>
>
>  def Print(self):
>   print self.group,':',self.name,':',self.url
>

this is my only major issue with your class design.  Depending on print
limits
your class's usefulness.  You are better off implenting something like your
asTuple() method and passing that to print:

print myclass.asString()

in Python a common way to accomplish this is via __str__ or __repr__:
>>> class Foo:
...   def __str__(self):
...     return "I am foo"
...
>>> f = Foo()
>>> print f
I am foo

Now your class can be used in cgi's, GUIs, text based programs, etc.

As a purely matter of style, your naming convention is reverse from what I
am
used to.  In projects I have worked on classes/types get capital letters,
methods as studlyCaps (note the first word is lower case) and variables get
names_like_this.  Often you will see functions declared like_this() as well.

class Bookmark:
  def __init__(self, group, name, url):
    self.group = group
    self.name = name
    self.url = url

  def __str__(self):
    return '%s:%s:%s' % (self.group, self.name, self.url)

  def asTuple(self):
    return (self.group, self.name, self.url)

class BookmarkGroupDb: pass








More information about the Python-list mailing list