title: How to set a class attribute from an instance method in python?
body: <p>For previous versions of the question see the <a href="https://stackoverflow.com/posts/25963577/revisions">revision history</a> - managed to come up with a minimal executable example that reproduces the problem</p>
  
  <pre><code># module test.py
  import shlex
  from test2 import Parser
  
  class Test(object):
      sync_client = None
  
      def __init__(self):
          self.__class__.sync_client = 1
          parser = Parser(description='desc') # was before the assignment - egal
          while True:
              cmd = shlex.split(raw_input('&gt; ').strip())
              parser.parse_args(cmd)
  
      @classmethod
      def newRequestClient(cls):
          print cls.sync_client # None
  
  if __name__ == "__main__":
      Test()
  
  # module test2.py
  import argparse
  
  class Share(object):
      class _ShareAction(argparse.Action):
          def __call__(self, parser, namespace, values, option_string=None):
              from test import Test
              print Test.sync_client # None
              Test.newRequestClient()
  
      def __call__(self, subparsers):
          parser_a = subparsers.add_parser('name')
          parser_a.add_argument(dest='dest', help='help2',
                                action=Share._ShareAction)
  
  class Parser(argparse.ArgumentParser):
      def __init__(self, description, add_h=True):
          super(Parser, self).__init__(description=description, add_help=add_h)
          subparsers = self.add_subparsers(title='Commands')
          subparsers._parser_class = argparse.ArgumentParser
          Share()(subparsers)
  </code></pre>
  
  <p>Run test.py and type name 123 in the prompt to see the Nones printed. Well maybe it is something obvious - it's been hours :)</p>
  
  <p><strong>EDIT:</strong> For the reproducer posted here this:</p>
  
  <pre><code>if __name__ == "__main__":
      from test import Test
      Test()
  </code></pre>
  
  <p>works. Adding <code>from sync import Sync</code> (and variations) in my main did not help however. I finally "solved" it by:</p>
  
  <pre><code>class Share(Command):
      class _ShareAction(argparse.Action):
          def __call__(self, parser, namespace, values, option_string=None):
              sync = sys.modules['__main__']
              print sync.Sync.sync_client # &lt;SyncClient(SyncClient, started 6084)&gt;
              sync.Sync.newRequestClient(host=namespace.host, repo=values)
  </code></pre>
  
  <p>But I do not yet fully understand why <code>from sync import Sync</code> did not work.</p>
  
  <p><strong>FINALLY</strong>: thanks to a <a href="https://stackoverflow.com/questions/25963577/how-to-set-a-class-attribute-from-an-instance-method-in-python/25963618?noredirect=1#comment40659902_25963618">comment</a> by @MartijnPieters:</p>
  
  <pre><code># module watcher.sync
  
  if __name__ == "__main__":
      from watcher.sync import Sync
      Sync().main()
  </code></pre>
  
  <p>This looks ugly though so feel free to comment (or even add an answer) on how I could avoid it.</p>