python 3 new to classes
Question: I'm .self taught in python and need some help with a really simple class I'm
trying to write. I would like to use everything outside of the InitLog class
in various modules WITHOUT needing to declare a new class variable everytime i
use it (eg i dont want to declare betty = Initlog in every (numerous) module i
wish to use the class ... is there a way i can do this? Thanks in advance

    
    
    import os
    import sys
    import pdb
    import fileinput
    import Tools
    
    
    class InitLog:
        def __init__(self):
            pass
        def Update(self):
            self.sound = sound
            self.engine = engine
    
        def save(self):
    
            self.summery = 'sound: ' + self.sound + '\n'
            self.summery += 'engine: ' + self.engine + '\n'
    
    
            #lock for use within threads
            Tools.Locked.State.LogAddress = False
            while Tools.Locked.State.LogAddress == True: pass
            Tools.Locked.State.LogAddress = True
    
            try: os.remove(path + '/' + self.dest + '/init.log')
            except: pass
    
            path = os.getcwd()
            if not os.path.exists(self.dest): os.makedirs(self.dest)
            if os.path.isfile(path + '/' + self.dest + '/init.log') == True: os.remove(path + '/' + self.dest + '/init.log')
            with open (path + '/' + self.dest + '/init.log', mode='a', encoding='utf-8') as a_file:
                a_file.write(self.summery)
            Tools.Locked.State.LogAddress = False
    
    
    
    InitLog.Update.sound = 'on'
    InitLog.Update.engine = 'google.txt'
    InitLog.save()
    

error: `TypeError: save() takes exactly 1 positional argument (0 given)`

**EDIT:**

I have shorted the above example to be less redundant.

Here is the working code i was previously using (before attepting my original
request to use InitLog without declaring a new class variable)

    
    
    import os
    import sys
    import pdb
    import fileinput
    import Tools
    
    
    class InitLog:
        def __init__(self):
            pass
        def setLaws(self):
            self.sound = 'off'
            self.engine = 'google.txt'
    
    
        def Update(self):
            while Tools.Locked.State.LogAddress == True: pass
            Tools.Locked.State.LogAddress = True
            try: os.remove(path + '/' + self.dest + '/init.log')
            except: pass
    
            summery = 'sound: ' + self.sound + '\n'
            summery += 'engine: ' + self.engine + '\n'
    
    
            path = os.getcwd()
            if not os.path.exists(self.dest): os.makedirs(self.dest)
            if os.path.isfile(path + '/' + self.dest + '/init.log') == True: os.remove(path + '/' + self.dest + '/init.log')
            with open (path + '/' + self.dest + '/init.log', mode='a', encoding='utf-8') as a_file:
                a_file.write(summery)
            Tools.Locked.State.LogAddress = False
    
    Tools.Locked.State.LogAddress = False
    log = InitLog()
    log.setLaws()
    log.sound = 'on'
    log.Update()
    

I would like to not need to say `log = InitLog()` for every module i need to
use this class


Answer: You are not properly using classes.

The following code:

    
    
    InitLog.Update.sound = 'on'
    InitLog.Update.engine = 'google.txt'
    InitLog.Update.countryspec = '.com'
    InitLog.Update.limit = '10'
    ....
    

is accessing the method object of Update of the class InitLog, and adding
attributes to it on the fly. This attributes are being attached to the
function object not the class or an object of that class.

`error: TypeError: save() takes exactly 1 positional argument (0 given)` save
is a method it requires a parameter self which is set implicitly when an
instance of an InitLog object calls it.

If you want a single instance through your code, then you can follow the
singleton design pattern.

first I recommend you go through
<http://docs.python.org/tutorial/classes.html>

Maybe you are tying to achieve something like this:

    
    
    class InitLog:
        sound = None
        engine = None
        summary = ""
        @classmethod
        def save(cls):
            cls.summary = 'sound: ' + cls.sound + '\n'
            cls.summary += 'engine: ' + cls.engine + '\n'
    
    InitLog.sound = 'On'
    InitLog.engine = 'google.txt'
    InitLog.save()
    print InitLog.summary 
    

Heres a different design which may be more reusable later on.

    
    
    class InitLog:
        custom_instance = None
        def __init__(self):
            self.sound = None
            self.engine = None
            self.summary = ''
        @property
        def sound(self):
            return self.sound
        @sound.setter
        def set_sound(self, sound):
            self.sound = sound
        @property
        def engine(self):
            return self.engine
        @engine.setter
        def set_engine(self, engine):
            self.engine = engine
        @property
        def summary(self):
            return self.summary
        @summary.setter
        def set_summary(self, summary):
            self.summary = summary
        def update(self, **kwargs):
            if 'sound' in kwargs:
                self.sound = kwargs['sound']
            if 'engine' in kwargs:
                self.engine = kwargs['engine']
        def save(self):
            self.summary = 'sound: ' + self.sound + '\n'
            self.summary += 'engine: ' + self.engine + '\n'
        @classmethod
        def get_custom_instance(cls):
            if not cls.custom_instance:
                cls.custom_instance = InitLoc()
                cls.custom_instance.engine = 'google.txt'
                cls.custom_instance.sound = 'on'
            return cls.custom_instance
    
    obj = InitLog.get_custom_instance()
    obj.save()
    print obj.summary  #ouput => 'sound: on\nengine: google.txt\n'
    print obj.update(sound = 'Off')
    obj.save()
    print obj.summary  #output =>'sound: Off\nengine: google.txt\n'
    

There are many ways of designing python classes or classes in general for that
matter, this are just 2, please go through the guide and find the best design
that suits your needs.

