473,796 Members | 2,512 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Factories in Python

I'm wondering how Python cognoscenti use/implement factory patterns in
Python. In my limited C++ experience, one reason to use a factory
method is to make it easier to create instances of classes derived off
a base class. The instance is declared to be an instance of the base
class. The factory returns an instance of a derived class, and
everything is groovy. In Python, since type declarations need not be
static, it seems there is no need to have a factory method.

However, I'm guessing that I'm not understanding other benefits of
using factories in Python. Does anybody have examples or pointers on
how use of factories in Python?

thanks
-robert
Jul 18 '05 #1
2 2257
Robert Ferrell wrote:

[snip]

However, I'm guessing that I'm not understanding other benefits of
using factories in Python. Does anybody have examples or pointers
on how use of factories in Python?


Here is a link:

http://aspn.activestate.com/ASPN/Coo...n/Recipe/86900

A reason for using a factory is that the client (the code that
creates the instance) does not know which class or sub-class it
wants to create an instance of. So, you build a factory that is
smart enough to create an instance of the right class.

In my case, while parsing an XML document and creating instances
of nodes in the tree that represents the document, the classes
(the super-class of each node class) does the parsing. However,
each super-class must be smart enough to create a tree populated
with instances of sub-classes, which may contain additional custom
code,*if* they are available.

I'd be interested in other reasons for using factories.

- Dave

--
Dave Kuhlman
http://www.rexx.com/~dkuhlman
dk******@rexx.c om
Jul 18 '05 #2
Dave Kuhlman wrote:
I'd be interested in other reasons for using factories.


My most frequent reason for using factories is to insulate client code from
refering directly to a class name. I find I do this most often when I have
code that's likely to change soon or change frequently. This approach
allows me to leave client code unchanged although the actual class names
and implementations may vary greatly.

Consider:

class MightBeWhatIWan t:
pass

class HelperForThat:
pass

def build():
return MightBeWhatIWan t()

As the problem is revealed thru elbow grease, this might change:

class ThisIsWhatIReal lyNeeded:
pass

class SomethingIMisse dEarlier:
pass

def build():
return ThisIsWhatIReal lyNeeded()

Granted, these aren't factories in the strictest GoF sense, but I think the
intent is clear.

troy
Jul 18 '05 #3

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

Similar topics

2
2202
by: threeseas | last post by:
Because the project is done in python and in support of FOSS http://msdn.microsoft.com/architecture/overview/softwarefactories/default.aspx?pull=/library/en-us/dnmaj/html/aj3softfac.asp "According to the Standish Group , businesses in the United States spend around $250 billion on software development each year on approximately 175,000 projects. Only 16 percent of these projects finish on schedule and within budget. Another 31 percent...
0
1055
by: Craig Ringer | last post by:
Hi folks I'm currently faced with the need to write a class factory in C using the Python/C API. It's purpose is to automatically wrap C++ objects at run-time and provide a Python object. Thanks to the magic of Qt's meta-object system, getting the information required to wrap the C++ object is the easy bit. What I'm not sure I can do is create 'built-in' Python classes at run time. I don't need to create classes in response to calls...
3
1414
by: Jeff Louie | last post by:
Well I finally finished a Chapter on the use of Type Based and Interface Based Class Factories in C# at: http://www.geocities.com/jeff_louie/OOP/oop18.htm Happy Holidays, Jeff *** Sent via Developersdex http://www.developersdex.com ***
1
1857
by: Mark F. Haigh | last post by:
In Andrei Alexandrescu's book _Modern C++ Design_, Chapter 8 ("Object Factories"), Section 8.4 ("Type Identifiers"), he states: The only problem that remains is the management of type identifiers. Still, adding type identifiers requires a fair amount of dicipline and centralized control. The only conclusion that can be drawn here is that type identifier management is not the business of the object factory itself.
1
3002
by: Patrick Stinson | last post by:
I am trying to create a way to register static members of a **class**, not an object, for making an object factory. The main thing I want is to be able to make a call like class MyClass { public: MyClass *factory(); }; register(MyClass);
5
2126
by: Jeff Greenberg | last post by:
Not an experienced c++ programmer here and I've gotten myself a bit stuck. I'm trying to implement a class lib and I've run into a sticky problem that I can't solve. I'd appreciate any help that I can get! Consider 3 classes in the following heirarchy: base / \ deriv1 deriv2 \
0
1116
by: Jeff Louie | last post by:
Well I finally finished a Chapter on the use of Type Based and Interface Based Factories at: http://www.geocities.com/jeff_louie/OOP/oop18.htm Happy Holidays, Jeff *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
1
1057
by: Smokey Grindle | last post by:
Any good examples out there of remoting with class factories out there? I want to use interfaces to keep my code server side but also need class factories to make the objects. havent figured out exactly how to do this.. thanks!
2
1729
by: werasm | last post by:
Hi all, I want to know whether the following is possible. I started out with an idea called a CreationAbstractor. Basically just a factory function wrapper for all types. It looks like this: template <class T> class CreationAbstractor
0
9673
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
9525
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
10452
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10169
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
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6785
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
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
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
3730
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.