Changing the private variables content

Ethan Furman ethan at stoneleaf.us
Wed Jul 22 18:35:01 EDT 2009


Ryniek90 wrote:
>>
>> Got it:
>>
>> exec('self.' + attr + '=\'' + val + '\'')
>>
>> That worked. I think it'll do what you want now ;)
>>
>> Ching-Yun "Xavier" Ho, Technical Artist
>>
>> Contact Information
>> Mobile: (+61) 04 3335 4748
>> Skype ID: SpaXe85
>> Email: contact at xavierho.com <mailto:contact at xavierho.com>
>> Website: http://xavierho.com/
> 
> 
> To bad, that didn't worked in my class. Still the same error:
> "
>  >>> mod.print_module('socket')
> 
> Traceback (most recent call last):
>  File "<pyshell#121>", line 1, in <module>
>    mod.print_module('socket')
>  File "<pyshell#118>", line 51, in print_module
>    module_open = open(self._this_module, 'rb')
> IOError: [Errno 2] No such file or directory: ''
>  >>>
> "
> 
> :-/
> 
>> What is the point of the _SetVar method?
>>
>> Instead of:
>>
>>     self._SetVar(self._this_module, os.path.join(root, f))
>>
>> just do:
>>
>>     self._this_module = os.path.join(root, f)
>>
>> (unless I'm misunderstanding what you're trying to do!)
>>
> 
> Of course i;ve tried, but still get the same error:
> 
> "
>  >>> mod.print_module('socket')
> 
> Traceback (most recent call last):
>  File "<pyshell#121>", line 1, in <module>
>    mod.print_module('socket')
>  File "<pyshell#118>", line 51, in print_module
>    module_open = open(self.this_module, 'rb')
> IOError: [Errno 2] No such file or directory: ''
>  >>>
> "
> 
> It looks like private variable have specific naure, that prevent from 
> traditional editing them.
> Still searching for some tuts about private methods and variables.

No.  There is nothing special about variables with a leading underscore.

_number

is treated by Python *exactly* the same as

number

.  The specific problem in your code above is your _SetVar function, 
among the more general problem of not yet having a good understanding of 
classes in Python.  Keep studying, Python is an awesome language.

I was able to make this work -- hope it helps.

8<-----------------------------------------------------------------------
import os
import sys

class ModPrint(object):
     u"""This will be the doc."""
     def __init__(self):
         self._default_search_path = sys.exec_prefix

     def _search_for_module(self, chosen_module, search_path):
         for root, dirs, files in os.walk(search_path):
             for f in files:
                 if f == ("%s.py" % chosen_module):
                     return os.path.join(root, f)

     def print_module(self, chosen_module, user_search_path=''):
         search_path = user_search_path or self._default_search_path
         module = self._search_for_module(chosen_module, search_path)
         if module is not None:
             module_open = open(module, 'rb')
             module_text = module_open.read()
             module_open.close()
             return module_text
         return 'Module not found...'
8<-----------------------------------------------------------------------



More information about the Python-list mailing list