Results 1 to 5 of 5

Thread: Initialization of variable in python?

  1. #1
    Join Date
    Sep 2006
    Beans
    42

    Initialization of variable in python?

    Hi!

    I'am new to python and there is one question that have not yet found an answer for. What should I initialize a class variable to?

    Many times now I have required a global class variable so I can use it
    in the other classes also. I initialize this variable at the top of the file and set it to None like this:

    ClassAObject=None

    Then in the main function I create the object like this:

    def main():
    global ClassAObject
    ....
    ...
    ClassAObject = ClassA()
    ...
    ..

    The in another class function I require the variable so I call it
    class ClassB()
    global ClassAObject
    ..
    ....
    def somefunc(self):
    ClassAObject.somotherfunc()

    This will cause an exception AttributeError: 'NoneType' object has no attribute 'soneotherfunc'. Is there a better way to initialize a global variable then None?

  2. #2
    Join Date
    Sep 2006
    Beans
    42

    Re: Initialization of variable in python?

    Sorry about this I found the error after reading this:

    "In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a new value anywhere within the function’s body, it’s assumed to be a local. If a variable is ever assigned a new value inside the function, the variable is implicitly local, and you need to explicitly declare it as global.

    Though a bit surprising at first, a moment’s consideration explains this. On one hand, requiring global for assigned variables provides a bar against unintended side-effects. On the other hand, if global was required for all global references, you’d be using global all the time. You’d have to declare as global every reference to a builtin function or to a component of an imported module. This clutter would defeat the usefulness of the global declaration for identifying side-effects." http://effbot.org/pyfaq/what-are-the...-in-python.htm

    Any thoughts about the use of global variables in python is appreciated.

  3. #3
    Join Date
    Jun 2006
    Location
    $ pwd _
    Beans
    3,999
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: Initialization of variable in python?

    If you are programming OOPS-way w/ classes, there will be no practical need for global variables.

  4. #4
    Join Date
    May 2008
    Location
    UK
    Beans
    1,451
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Initialization of variable in python?

    One technique i use is a local variable in a module as the class, with a factory function to create or provide the reference to the instance

    Code:
    instance = None
    
    def GetSingleInstance():
        if instance is None:
            instance = MyClass()
         return instance
    
    
    class MyClass():
        def __init(self)__:
            pass
    
        def do_stuff():
            pass
    This way although effectively you have a global, it is hidden behind a factory function, and the factory function is the thing that is in global scope : as "module.GetSingleInstance() - just need to make sure that all of your code calls the factory function.

    You can extend this to provide things like names against objects, so you can refer to an object with a string (or number) - basically the factory function maintains the dictionary.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  5. #5

    Re: Initialization of variable in python?

    Quote Originally Posted by Tony Flury View Post
    One technique i use is a local variable in a module as the class, with a factory function to create or provide the reference to the instance
    Using a singleton is probably more OO friendly. You can find plenty of python singleton implementations on Google.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •