#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    California, USA
    Posts
    76
    Rep Power
    2

    Called Module passing back a String to the Caller


    How do you return a string from code in the called module back
    to the the calling module, as in the two modules below?
    The code produces this:
    results: <testcode2.called_class object at 0x02292290>

    Code:
    #testcode1.py
    import testcode2
    
    class main_class():
    
        def __init__(self):
    
            one_pie = 'pie'
            
            x_pies = testcode2.called_class(one_pie, 10) 
    
            print('results: ', x_pies)
            
    main_class()
    Code:
    #testcode2.py
    
    class called_class():
    
        def __init__(self, inarg, dupfactor):
        
            return_arg = (inarg + '*') * dupfactor
            
            return return_arg
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480

    Separate modules unrelated to actual problem.


    When python creates an object, in your code you've called
    CLASS(init_args)
    , python returns that object, verifying the value returned from __init__ method is None. Certainly true in python 3, I had thought python 2 also performs this test but you say your program worked in the sense of "no exceptions".

    The built in str class accepts any object and tries to create a string from it. str calls the object's __str__ method, if it has one. If not, it returns repr(obj) . The default __repr__ method returns the pretty combination of the object's class name and its id().

    You now realize that you need a program like
    Code:
    class Main:
    
        def __init__(self):
            one_pie = 'pie'
            x_pies = Subordinate(one_pie, 10) 
            print('results: ', x_pies)
    
    class Subordinate:
    
        def __init__(self, inarg, dupfactor):
            self.str = (inarg + '*') * dupfactor
    
        def __str__(self):
            return self.str
    
    
    Main() # creating an object of type Main runs Main.__init__
    and you can spread these classes amongst modules as you please.


    (python2 does test __init__ return value. You lied or are not using a conformal python.)
    Last edited by b49P23TIvg; September 23rd, 2013 at 07:49 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    California, USA
    Posts
    76
    Rep Power
    2
    The __str__ method worked and I see that it can be used
    to pass a string from code in a separate module back to the
    calling module.

    I am using Python 3.3.0. Any mis-information on my part was
    not intentional. I did not mention that at the beginning I got
    this error, then at a point it stopped and I was consistently
    getting the result I posted. Now, this morning when I run the
    code I posted here, I get the error that I saw yesterday:
    Traceback (most recent call last):
    File "C:\Python33\Lib\idlelib\MyPgms\testcode1.py", line 14, in <module>
    main_class()
    File "C:\Python33\Lib\idlelib\MyPgms\testcode1.py", line 10, in __init__
    x_pies = testcode2.called_class(one_pie, 10)
    TypeError: __init__() should return None, not 'str'

    Your solution takes care of the problem. Thanks.
    Last edited by pyJer; September 23rd, 2013 at 10:27 AM.

IMN logo majestic logo threadwatch logo seochat tools logo