initialization in python

koranthala at gmail.com koranthala at gmail.com
Fri Jan 2 00:48:34 EST 2009


On Jan 1, 11:14 pm, Terry Reedy <tjre... at udel.edu> wrote:
> koranth... at gmail.com wrote:
> >>> Module database:
> >>> ^^^^^^^^^^^^^^^^^^^
> >>> Session = None
>
> 'Initializing' names is not necessary.  Delete this.  Without it, your
> error would be more obvious.
>
> >>> def init(dbname):
> >>>    engine = create_engine('sqlite:///%s' %dbname)
> >>>    ...
> >>>    global Session
> >>>    Session = sessionmaker(bind=engine)
>
> This **rebinds* the module name 'Session' to a new object, making the
> initial bindin irrelevant and misleading.
>
> [snip]
>
>
>
> > I guessed as much. But, I was under the impression that if the
> > original value is modified, the variables value also will change.
>
> Python has names and slots bound to objects.  Some objects are mutable
> and some not.  None is not mutable.  You replaced it.
>
> If you had done something like
>
> Session = sessionmaker()
>
> def init(dbname):
>      Session.engine = create_engine('sqlite:///%s' %dbname)
>      ...
>
> then you would have *modified* the original object (value) bound to
> 'Session' and would have seen the behavior you expected.
>
> Terry Jan Reedy

I did not think about it. I was using database.Session till now.
I have now modified the code.
Thank you very much, Terry Jan Reedy



More information about the Python-list mailing list