one-time factory in python for an experienced java guy
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: one-time factory in python for an experienced java guy
Stefan, thanks first of all Use a global variable in the module. I have an account_number_generator variable in the module, I got that hint from searching the web. But where my stubborn java mind doesn't release me: what does the variable contain? Do I create the actual IncrementalGenerator object there? Or the super class? Or just a string, which a factory method takes to create the actual object? What I especially don't get: How will an external module overwrite that variable? I fail to see, python being interpreted, how I can ensure that a future module is being executed later, thus overwriting the variable. Thanks again. -- http://mail.python.org/mailman/listinfo/python-list
Re: one-time factory in python for an experienced java guy
Thanks Paul, Ugh, just forget everything you ever knew about java. Do some Zen exercises to erase your mind. Then read a Python tutorial as if you're starting from nothing. Yeah, surely right, but easier said than done... I'm working on it. Taking your example. import itertools class Account(object): def __init__(self, holder, gen=itertools.count()): self.__accountnumber = gen.next() If you consider my python illiteracy, "itertools.count(): Make an iterator that returns consecutive integers starting with n" to me that sounds like that solves the increment issue, but what about future modules wanting to plug in a different numbering format, e.g. 205434.1234 or whatever? -- http://mail.python.org/mailman/listinfo/python-list
Re: one-time factory in python for an experienced java guy
Thanks for all replies. I need to practice much more pythonese In fact I don't think to understand all of your suggestions, so I'll need to go through them and decide what approach I am going to take. Thanks a lot! -- http://mail.python.org/mailman/listinfo/python-list
