473,396 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 2234
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.com
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 MightBeWhatIWant:
pass

class HelperForThat:
pass

def build():
return MightBeWhatIWant()

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

class ThisIsWhatIReallyNeeded:
pass

class SomethingIMissedEarlier:
pass

def build():
return ThisIsWhatIReallyNeeded()

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
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 ...
0
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...
3
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...
1
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...
1
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 {...
5
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...
0
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...
1
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...
2
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:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.