Class Python3类的新特性

Class Python3类的新特性,class,function,python-3.x,Class,Function,Python 3.x,我自学了python,需要一些帮助来学习一门非常简单的课程。我想在各种模块中使用InitLog类之外的所有东西,而不需要每次使用它时都声明一个新的类变量。例如,我不想在我希望使用该类的每个模块中声明betty=InitLog。。。我有办法做到这一点吗?提前谢谢 import os import sys import pdb import fileinput import Tools class InitLog: def __init__(self): pass

我自学了python,需要一些帮助来学习一门非常简单的课程。我想在各种模块中使用InitLog类之外的所有东西,而不需要每次使用它时都声明一个新的类变量。例如,我不想在我希望使用该类的每个模块中声明betty=InitLog。。。我有办法做到这一点吗?提前谢谢

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()
错误:TypeError:save只接受给定的1个位置参数0

编辑:

我缩短了上面的示例,以减少冗余

下面是我在尝试使用InitLog而不声明新类变量的原始请求之前使用的工作代码

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()

对于我需要使用此类的每个模块,我不想说log=InitLog,因为您没有正确使用类

以下代码:

InitLog.Update.sound = 'on'
InitLog.Update.engine = 'google.txt'
InitLog.Update.countryspec = '.com'
InitLog.Update.limit = '10'
....
正在访问类InitLog的Update的方法对象,并动态地向其添加属性。这些属性被附加到函数对象,而不是类或该类的对象

错误:TypeError:save只接受1个位置参数0如果save是一个方法,它需要一个参数self,该参数在InitLog对象的实例调用它时隐式设置

如果希望代码中有一个实例,那么可以遵循单例设计模式

首先我建议你通过

也许你正在努力实现以下目标:

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 
这里有一个不同的设计,以后可能更易于重用

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'

设计python类或类的方法有很多,这只是2种,请仔细阅读指南,找到适合您需要的最佳设计。

使用类几乎是错误的。我建议您首先回去阅读有关类、实例化和方法调用的内容。将有助于上面的保存和更新,但正如Keith所说,您在该代码上还有其他问题嘿Mike,在决定不在每个模块上声明InitLog之前,我已经包含了我的原始工作代码。您需要仔细阅读他的问题。。。我想在各个模块中使用InitLog类之外的所有东西,而不需要声明新的类变量。每次我使用itthanks作为回复时,在决定不在每个模块上声明InitLog之前,我已经包含了我的原始工作代码。正如您所看到的,它包含了对您所做的InitLog的更精简的声明