one-time factory in python for an experienced java guy

pdpi pdpinheiro at gmail.com
Tue Jul 14 11:35:40 EDT 2009


On Jul 14, 3:03 pm, phonky <pho... at europe.com> wrote:
> Hi
>
> I have searched all over and haven't found the solution
> for my problem yet. I am new to python, and all the time realize I
> do program python in java, which is not great.
>
> Besides being a real-life problem, I want to
> solve it as elegant as I can, using it to
> also learn about python (I know I could just
> hack something easy for now).
>
> That's what I want to do.
>
> I have an Account class.
> An Account instance has to get an account number.
> On instance creation, I want the account number to
> be generated (I don't want callers to pass
> the account # to ensure uniqueness):
>
> class Account(object):
>         def __init__(self, holder):
>                 self.__accountnumber = self.__generate_account_number()
>
> Now, I do not know yet how the account number scheme looks like.
> For now, I just want to have an incremental number; later,
> when going to production, we'll need the proper one.
>
> Furthermore, as we plan to distribute the package, we want
> to allow custom account numbering schemes. Thus, customers
> should be able to plug in their own AccountNumberGenerator implementation.
>
> For now, I have a generators.py module.
>
> I have an AccountNumberGenerator base class,
> all subclasses should implement "generate".
>
> I have an IncrementalGenerator subclass.
>
> So for now, I need to instantiate IncrementalGenerator,
> allowing for a future module to plugin their own generator.
>
> Any suggestions on how to do this?
> Thanks so much!!!!

You don't want an AccountNumberGenerator class and subclassing, all
you need is an iterator/generator of some form.

These might help:
http://docs.python.org/tutorial/classes.html#iterators
http://docs.python.org/tutorial/classes.html#generators
(in fact, that whole page is pretty relevant)

Once your code expects one of those, it's trivially easy to plug
something else in there.



More information about the Python-list mailing list