473,769 Members | 6,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why a class when there will only be one instance?

I see the value of a class when two or more instances will be created,
but Python programmers regularly use a class when there will only be one
instance.
What is the benefit of this? It has a disadvantage of a whole lot of
"self."
being required everywhere, making the code less readable. Also, since a
strength of Python is rapid application development, it slows one down
to have to put in all those self.'s. The code seems much cleaner to me
without classes that have only one instance. Oh, also, all the methods
of this class will have to have the instance name prepended to them.

I would appreciate it if someone could explain the advantages of doing
this, or at least the sociological reasons why it occurs.

Mitchell Timin

--
"Many are stubborn in pursuit of the path they have chosen, few in
pursuit of the goal." - Friedrich Nietzsche

http://annevolve.sourceforge.net is what I'm into nowadays.
Humans may write to me at this address: zenguy at shaw dot ca
Jul 18 '05 #1
34 4767
On Ter 25 Mai 2004 21:43, Se******@SeeBel ow.Nut wrote:
I see the value of a class when two or more instances will be created,
but Python programmers regularly use a class when there will only be one
instance.
What is the benefit of this? It has a disadvantage of a whole lot of
"self."
being required everywhere, making the code less readable. Also, since a
strength of Python is rapid application development, it slows one down
to have to put in all those self.'s. The code seems much cleaner to me
without classes that have only one instance. Oh, also, all the methods
of this class will have to have the instance name prepended to them.

I would appreciate it if someone could explain the advantages of doing
this, or at least the sociological reasons why it occurs.


How can you guarantee that such a code will never ever be used again,
anywhere?

It's easier to allow for code reuse from the beginning, IMHO.

--
Godoy. <go***@ieee.org >
Jul 18 '05 #2
On 2004-05-26, Se******@SeeBel ow.Nut <Se******@SeeBe low.Nut> wrote:
I see the value of a class when two or more instances will be created, but
Python programmers regularly use a class when there will only be one
instance. What is the benefit of this?


What happens when down the line someone wants to use that code in
something that requires multiple instances?

--
Steve C. Lamb | I'm your priest, I'm your shrink, I'm your
PGP Key: 8B6E99C5 | main connection to the switchboard of souls.
-------------------------------+---------------------------------------------
Jul 18 '05 #3
On Wed, 26 May 2004 00:43:49 +0000, SeeBelow wrote:
I see the value of a class when two or more instances will be created,
but Python programmers regularly use a class when there will only be one
instance.
What is the benefit of this? It has a disadvantage of a whole lot of
"self."
being required everywhere, making the code less readable. Also, since a
strength of Python is rapid application development, it slows one down
to have to put in all those self.'s. The code seems much cleaner to me
without classes that have only one instance. Oh, also, all the methods
of this class will have to have the instance name prepended to them.

I would appreciate it if someone could explain the advantages of doing
this, or at least the sociological reasons why it occurs.

Mitchell Timin


defining a class may be useful if you plan on making more instances down
the line. It's a good OO strategy. I do understand your dislike of 'self'.
It does seem like clutter. In my code, I shorten it to 's'. In ruby, class
variables are prefixed with an '@', which makes them easier to discern in
code, and it is easier than typing 'self'. I wish python had something
like that. I also think that having to specify the class variable in every
function definition is a bit silly. Ruby gets rid of that too.

--SegPhault

Jul 18 '05 #4
In article <40************ ***@shaw.ca>, Se******@SeeBel ow.Nut wrote:
I see the value of a class when two or more instances will be created,
but Python programmers regularly use a class when there will only be one
instance.
What is the benefit of this? It has a disadvantage of a whole lot of
"self."
being required everywhere, making the code less readable. Also, since a
strength of Python is rapid application development, it slows one down
to have to put in all those self.'s. The code seems much cleaner to me
without classes that have only one instance. Oh, also, all the methods
of this class will have to have the instance name prepended to them.

I would appreciate it if someone could explain the advantages of doing
this, or at least the sociological reasons why it occurs.

Mitchell Timin


Typing "self" is a mechanical process which adds very little to the
development cost. Deciding which things to make classes and which not
to requires significant mental effort and does add cost. It's just
easier to make everything a class.

More than that, most times I've decided to not bother making something a
class because it was too simple, I've eventually added enough
functionality to it to change my mind and have to re-do things. That's
real cost. Much simplier and cheaper to just make it a class from the
get-go.
Jul 18 '05 #5
Ryan Paul <se*******@sbcg lobal.net> wrote:
defining a class may be useful if you plan on making more instances down
the line. It's a good OO strategy. I do understand your dislike of 'self'.
It does seem like clutter. In my code, I shorten it to 's'.


Please don't do that. While it's true that the first parameter of a
class method can be named anything, the use of "self" is so
overwhelmingly ubiquitous it might as well be a standard. Using
anything else is just going to make your code more difficult for anybody
else to read and understand.

Typing is cheap. Thinking is expensive. And, yes Aahz, you can quote
me on that :-)
Jul 18 '05 #6
Roy Smith wrote:

In article <40************ ***@shaw.ca>, Se******@SeeBel ow.Nut wrote:
I see the value of a class when two or more instances will be created,
but Python programmers regularly use a class when there will only be one
instance.
What is the benefit of this? It has a disadvantage of a whole lot of
"self."
being required everywhere, making the code less readable. Also, since a
strength of Python is rapid application development, it slows one down
to have to put in all those self.'s. The code seems much cleaner to me
without classes that have only one instance. Oh, also, all the methods
of this class will have to have the instance name prepended to them.

I would appreciate it if someone could explain the advantages of doing
this, or at least the sociological reasons why it occurs.

Mitchell Timin
Typing "self" is a mechanical process which adds very little to the
development cost. Deciding which things to make classes and which not
to requires significant mental effort and does add cost. It's just
easier to make everything a class.


Even easier is not to make anything a class unless there will be two or
more instances of it. I still don't get what advantage making a class
buys for you.

More than that, most times I've decided to not bother making something a
class because it was too simple, I've eventually added enough
functionality to it to change my mind and have to re-do things. That's
real cost. Much simplier and cheaper to just make it a class from the
get-go.


Why does greater functionality make a class desireable, if there won't
be multiple instances created?

Other people have mentioned "code reuse". Again I don't see how a class
helps to make code reusable. I find methods in a class more difficult
to reuse than simple function definitions. (unless there are multiple
instances.)

Mitchell Timin

--
"Many are stubborn in pursuit of the path they have chosen, few in
pursuit of the goal." - Friedrich Nietzsche

http://annevolve.sourceforge.net is what I'm into nowadays.
Humans may write to me at this address: zenguy at shaw dot ca
Jul 18 '05 #7
In article <40************ ***@shaw.ca>, Se******@SeeBel ow.Nut wrote:
More than that, most times I've decided to not bother making something a
class because it was too simple, I've eventually added enough
functionality to it to change my mind and have to re-do things. That's
real cost. Much simplier and cheaper to just make it a class from the
get-go.


Why does greater functionality make a class desireable, if there won't
be multiple instances created?


For me, it's more about encapsulation than code re-use. If I've got a
bunch of functions which operate on a collection of data, to me that
says "object", which in Python (and most OOPL's) implies "class".

Bundling it up into a class lets me think about it as a unit. Each
class is a convenient thought unit for design and testing, and also for
understanding somebody else's code.

I don't think there's a need to be dogmatic about it. It's just what I
find is a natural way to break a program down into smaller pieces you
can get your brain around one piece at a time.
Jul 18 '05 #8
Roy Smith wrote:

In article <40************ ***@shaw.ca>, Se******@SeeBel ow.Nut wrote:
More than that, most times I've decided to not bother making something a
class because it was too simple, I've eventually added enough
functionality to it to change my mind and have to re-do things. That's
real cost. Much simplier and cheaper to just make it a class from the
get-go.
Why does greater functionality make a class desireable, if there won't
be multiple instances created?


For me, it's more about encapsulation than code re-use. If I've got a
bunch of functions which operate on a collection of data, to me that
says "object", which in Python (and most OOPL's) implies "class".


OK, that makes some sense - to associate certain data and code items and
separate them from other code and data. Wouldn't that purpose be served
even better by putting them into a different file, and not bother with a
class?
Bundling it up into a class lets me think about it as a unit. Each
class is a convenient thought unit for design and testing, and also for
understanding somebody else's code.

I don't think there's a need to be dogmatic about it. It's just what I
find is a natural way to break a program down into smaller pieces you
can get your brain around one piece at a time.

--
"Many are stubborn in pursuit of the path they have chosen, few in
pursuit of the goal." - Friedrich Nietzsche

http://annevolve.sourceforge.net is what I'm into nowadays.
Humans may write to me at this address: zenguy at shaw dot ca
Jul 18 '05 #9
<Se******@SeeBe low.Nut> wrote in message news:40******** *******@shaw.ca ...
I see the value of a class when two or more instances will be created,
but Python programmers regularly use a class when there will only be one
instance.
What is the benefit of this? It has a disadvantage of a whole lot of
"self."
being required everywhere, making the code less readable. Also, since a
strength of Python is rapid application development, it slows one down
to have to put in all those self.'s. The code seems much cleaner to me
without classes that have only one instance. Oh, also, all the methods
of this class will have to have the instance name prepended to them.
I'm only aware of one domain (interactive fiction) where it is really
common to have single instance classes. That's served by a number
of special purpose languages (Inform, TADS and Hugo spring to
mind). Your example (of single use classes in a UI context) seems
to be a bit contrived. I've usually found that I could get quite a bit
of code reuse out of UI classes simply by thinking the problems
through.

Granted, single use classes aren't uncommon, but instances of
other classes are much more common in most programs. In
Python, there are a number of ways of getting singletons without
very much extra effort.
I would appreciate it if someone could explain the advantages of doing
this, or at least the sociological reasons why it occurs.
Both have their uses. The class / instance dicotomy comes from
the earliest days of object oriented programming, and experiments
with classless languages (see the Prothon stuff currently going on,
also the IO language) have never been very compelling. A lot of
people (which include me) think that it's much to easy to get sloppy
with that paradigm.

The other thing to understand is that in most OO languages a
class is a declaration: it does not appear in the actual object
program other than as a framework for the instances that are
created. Python is one of the few languages where classes are
first class objects.

John Roth

Mitchell Timin

Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
8580
by: Sibyl | last post by:
Is there any way to get the name of a class without an instance (i.e., object of the class)? I am working with log4j, and would like a uniform way to name loggers without typing in the name of the class for each individual class. For example, ------------ import org.apache.log4j.Logger; public class Foo { ...
18
6956
by: John M. Gabriele | last post by:
I've done some C++ and Java in the past, and have recently learned a fair amount of Python. One thing I still really don't get though is the difference between class methods and instance methods. I guess I'll try to narrow it down to a few specific questions, but any further input offered on the subject is greatly appreciated: 1. Are all of my class's methods supposed to take 'self' as their first arg? 2. Am I then supposed to call...
4
1907
by: Donnal Walter | last post by:
This is a question about Python patterns or idioms. Over a period of time, I have evolved a pattern of usage that seems to work better for me than other ways I tried previously, but in writing some documentation I don't know what to call this syntax or how best to describe it. I have not seen it used in other places. The somewhat longish version is that I have implemented an MVP (model-view-presenter) architecture where each "presenter"...
9
2158
by: flarkblark | last post by:
I recently had the displeasure of looking at the code required to implement the pop-up menus used in a pulldown menu system for a webpage. The sheer amount of Javascript required was amazing and frankly revolting. It is as though the software "engineers" have been thrown out and replaced with "programmers". That is to say, it is a sophomoric hack job, a hideous kludge.
1
2525
by: Belebele | last post by:
I would like to have the compiler bind a Base class const reference to a temporary object of a Derived class when calling another class constructor. Please see the code below: class Base {}; class Derived: public Base {}; class Foo { public:
5
1420
by: Yarco | last post by:
When using c++, we could do: using abc = xxx.yyy; // am i right? When using python we could do: import abc.efg as efg When will php support: class My_Best_Class {
1
1271
by: hhhhhhhh | last post by:
What is a class variable or instance variable? I am still confused. Suppose I have the code like this: public class SavingAccount { private double annualInterestRate = 0; private double savingsBalance; }
2
1518
by: Damfino | last post by:
Hi all, Newbie question here wrt defining a class that will work on bits read from a binary file. How would you go about doing it? As an example please look at the structure of my data given below. The data comes in 40 byte packets via stdin or a binary file. my_Data_pkt(){ syncByte (8bits) XML_type (2bits) XML_subtype (2bits) record_value (3bits)
4
1686
by: Ken Fine | last post by:
I know about this: http://silverlight.net/forums/14.aspx and this: http://forums.asp.net/1127.aspx Silverlight Beta 2 was announced at TechEd. I was impressed. When does Silverlight get a first-class newsgroup? -KF
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10050
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9999
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.