NameError question - def(self,master) - master not in namespacewithin class?

Discussion in 'Python' started by harijay, Oct 9, 2008.

  1. harijay

    harijay Guest

    Hi I am new to writing module and object oriented python code. I am
    trying to understand namespaces and classes in python.

    I have the following test case given in three files runner , master
    and child. I am getting an error within child where in one line it
    understands variable master.name and in the next line it gives a
    NameError as given here

    " print "Reset name now from %s to %s , oldname %s is saved in
    mastertrash" % (master.trash, master.name , master.trash)
    NameError: name 'master' is not defined"


    Sorry for a long post because I dont know how to frame my question.
    I am pasting the code contained in the three files and the error
    message here
    Thanks for your help
    harijay


    The detailed error I get is
    hazel:tmp hari$ python runner.py
    Traceback (most recent call last):
    File "runner.py", line 3, in <module>
    import child
    File "/Users/hari/rpc-ccp4/tmp/child.py", line 1, in <module>
    class child():
    File "/Users/hari/rpc-ccp4/tmp/child.py", line 9, in child
    print "Reset name now from %s to %s , oldname %s is saved in
    mastertrash" % (master.trash, master.name , master.trash)
    NameError: name 'master' is not defined

    #File runner.py
    #!/usr/bin/python
    import master
    import child

    if __name__=="__main__":
    print "RUNNING RUNNER"
    m = master.master("hj","oldhj")
    s = child.child(m)
    print "Now I have the variable master name %s and master.trash %s" %
    (m.name , m.trash)

    #File master.py
    class master():
    name=""
    trash=""

    def __init__(self,name,trash):
    self.name = name
    self.trash = trash


    #File child.py
    class child():
    def __init__(self,master):
    print "Master name is %s" % master.name
    print "Now seeting master name to setnameinchild in child.py "
    tmp = master.trash
    master.trash = master.name
    master.name = "setnameinchild"
    print "Reset name now from %s to %s , oldname %s is saved in
    mastertrash" % (master.trash, master.name , master.trash)
    harijay, Oct 9, 2008
    #1
    1. Advertising

  2. harijay

    Guest

    On 9 Ott, 17:43, harijay <> wrote:
    > Hi I am new to writing module and object oriented python code. I am
    > trying to understand namespaces and classes in python.
    >
    > I have the following test case given in three files runner , master
    > and child. I am getting an error within child where in one line it
    > understands variable master.name and in the next line it gives a
    > NameError as given here
    >
    > "    print "Reset name now from %s to %s , oldname %s is saved in
    > mastertrash" % (master.trash, master.name , master.trash)
    > NameError: name 'master' is not defined"
    >
    > Sorry for a long post because I dont know how to  frame my question.
    > I am pasting the code contained in the three files and the error
    > message here
    > Thanks for your help
    > harijay
    >
    > The detailed error I get is
    > hazel:tmp hari$ python runner.py
    > Traceback (most recent call last):
    >   File "runner.py", line 3, in <module>
    >     import child
    >   File "/Users/hari/rpc-ccp4/tmp/child.py", line 1, in <module>
    >     class child():
    >   File "/Users/hari/rpc-ccp4/tmp/child.py", line 9, in child
    >     print "Reset name now from %s to %s , oldname %s is saved in
    > mastertrash" % (master.trash, master.name , master.trash)
    > NameError: name 'master' is not defined
    >
    > #File runner.py
    > #!/usr/bin/python
    > import master
    > import child
    >
    > if __name__=="__main__":
    >         print "RUNNING RUNNER"
    >         m = master.master("hj","oldhj")
    >         s = child.child(m)
    >         print "Now I have the variable master name %s and master.trash %s" %
    > (m.name , m.trash)
    >
    > #File master.py
    > class master():
    >         name=""
    >         trash=""
    >
    >         def __init__(self,name,trash):
    >                 self.name = name
    >                 self.trash = trash
    >
    > #File child.py
    > class child():
    >         def __init__(self,master):
    >                 print "Master  name is %s" % master.name
    >                 print  "Now seeting master name to setnameinchild in child.py "
    >                 tmp = master.trash
    >                 master.trash = master.name
    >                 master.name = "setnameinchild"
    >         print "Reset name now from %s to %s , oldname %s is saved in
    > mastertrash" % (master.trash, master.name , master.trash)



    You need to have an import master in child.py too.

    Ciao
    -----
    FB

    Ciao
    ----
    FB
    , Oct 9, 2008
    #2
    1. Advertising

  3. harijay

    Jerry Hill Guest

    Re: NameError question - def(self,master) - master not in namespace within class?

    On Thu, Oct 9, 2008 at 11:43 AM, harijay <> wrote:
    > Hi I am new to writing module and object oriented python code. I am
    > trying to understand namespaces and classes in python.
    >
    > I have the following test case given in three files runner , master
    > and child. I am getting an error within child where in one line it
    > understands variable master.name and in the next line it gives a
    > NameError as given here
    >


    <snip>

    > #File child.py
    > class child():
    > def __init__(self,master):
    > print "Master name is %s" % master.name
    > print "Now seeting master name to setnameinchild in child.py "
    > tmp = master.trash
    > master.trash = master.name
    > master.name = "setnameinchild"
    > print "Reset name now from %s to %s , oldname %s is saved in
    > mastertrash" % (master.trash, master.name , master.trash)


    The last line (starting with print), is not indented to the correct
    level. Assuming it's supposed to be part of the child class's
    __init__ method, it needs to line up with the rest of that method.

    --
    Jerry
    Jerry Hill, Oct 9, 2008
    #3
  4. harijay

    harijay Guest

    Thanks beiff for your prompt reply - But I shouldnt need to import
    master in child.

    Actually and very strangely. The problem fixed itself without any
    change to my code.
    I dont understand how. It may have been a problem with a bad *.pyc
    lingering around . But now I cannot get the old NameError to repeat
    itself..its very weird
    Here is the new code and it runs just fine ..I am absolutely confused
    as to why it failed several times in a row and then fixed itself.


    Here is the new code just to convince myself that there is absolutely
    no change;


    #File runner.py
    #!/usr/bin/python
    import master
    import child

    if __name__=="__main__":
    print "RUNNING RUNNER"
    m = master.master("hj","oldhj")
    s = child.child(m)
    print "Now I have the variable master name %s and master.trash %s" %
    (m.name , m.trash)


    #File child.py
    class child():

    def __init__(self,master):
    print "Master name is %s" % master.name
    print "Now seeting master name to setnameinchild in child.py "
    tmp = master.trash
    master.trash = master.name
    master.name = "setnameinchild"
    print "Reset name now from %s to %s , oldname %s is saved in
    mastertrash" % (master.trash, master.name , master.trash)

    #File master.py
    class master():
    name=""
    trash=""

    def __init__(self,name,trash):
    self.name = name
    self.trash = trash

    Produces the output:
    hazel:tmp hari$ python runner.py
    RUNNING RUNNER
    Master name is hj
    Now seeting master name to setnameinchild in child.py
    Reset name now from hj to setnameinchild , oldname hj is saved in
    mastertrash
    Now I have the variable master name setnameinchild and master.trash hj

    On Oct 9, 11:54 am, wrote:
    > On 9 Ott, 17:43, harijay <> wrote:
    >
    >
    >
    > > Hi I am new to writing module and object oriented python code. I am
    > > trying to understand namespaces and classes in python.

    >
    > > I have the following test case given in three files runner , master
    > > and child. I am getting an error within child where in one line it
    > > understands variable master.name and in the next line it gives a
    > > NameError as given here

    >
    > > "    print "Reset name now from %s to %s , oldname %s is saved in
    > > mastertrash" % (master.trash, master.name , master.trash)
    > > NameError: name 'master' is not defined"

    >
    > > Sorry for a long post because I dont know how to  frame my question.
    > > I am pasting the code contained in the three files and the error
    > > message here
    > > Thanks for your help
    > > harijay

    >
    > > The detailed error I get is
    > > hazel:tmp hari$ python runner.py
    > > Traceback (most recent call last):
    > >   File "runner.py", line 3, in <module>
    > >     import child
    > >   File "/Users/hari/rpc-ccp4/tmp/child.py", line 1, in <module>
    > >     class child():
    > >   File "/Users/hari/rpc-ccp4/tmp/child.py", line 9, in child
    > >     print "Reset name now from %s to %s , oldname %s is saved in
    > > mastertrash" % (master.trash, master.name , master.trash)
    > > NameError: name 'master' is not defined

    >
    > > #File runner.py
    > > #!/usr/bin/python
    > > import master
    > > import child

    >
    > > if __name__=="__main__":
    > >         print "RUNNING RUNNER"
    > >         m = master.master("hj","oldhj")
    > >         s = child.child(m)
    > >         print "Now I have the variable master name %s and master.trash %s" %
    > > (m.name , m.trash)

    >
    > > #File master.py
    > > class master():
    > >         name=""
    > >         trash=""

    >
    > >         def __init__(self,name,trash):
    > >                 self.name = name
    > >                 self.trash = trash

    >
    > > #File child.py
    > > class child():
    > >         def __init__(self,master):
    > >                 print "Master  name is %s" % master.name
    > >                 print  "Now seeting master name to setnameinchild in child.py "
    > >                 tmp = master.trash
    > >                 master.trash = master.name
    > >                 master.name = "setnameinchild"
    > >         print "Reset name now from %s to %s , oldname %s is saved in
    > > mastertrash" % (master.trash, master.name , master.trash)

    >
    > You need to have an import master in child.py too.
    >
    > Ciao
    > -----
    > FB
    >
    > Ciao
    > ----
    > FB
    harijay, Oct 9, 2008
    #4
  5. harijay

    harijay Guest

    Re: NameError question - def(self, master) - master not in namespacewithin class?

    Thanks Jerry and beiff ,
    Jerry was right, it was an indent problem . Between using my text
    editor and running from commandline something went out of sync and I
    didnt catch it probably.
    I can now reproduce the error with a bad ident .

    These are my first posts to comp.lang.python..and I am very grateful
    to everyone for their time .

    harijay

    On Oct 9, 12:07 pm, "Jerry Hill" <> wrote:
    > On Thu, Oct 9, 2008 at 11:43 AM, harijay <> wrote:
    > > Hi I am new to writing module and object oriented python code. I am
    > > trying to understand namespaces and classes in python.

    >
    > > I have the following test case given in three files runner , master
    > > and child. I am getting an error within child where in one line it
    > > understands variable master.name and in the next line it gives a
    > > NameError as given here

    >
    > <snip>
    >
    > > #File child.py
    > > class child():
    > >        def __init__(self,master):
    > >                print "Master  name is %s" % master.name
    > >                print  "Now seeting master name to setnameinchild in child.py "
    > >                tmp = master.trash
    > >                master.trash = master.name
    > >                master.name = "setnameinchild"
    > >        print "Reset name now from %s to %s , oldname %s is saved in
    > > mastertrash" % (master.trash, master.name , master.trash)

    >
    > The last line (starting with print), is not indented to the correct
    > level.  Assuming it's supposed to be part of the child class's
    > __init__ method, it needs to line up with the rest of that method.
    >
    > --
    > Jerry
    harijay, Oct 9, 2008
    #5
  6. a écrit :
    <OT>
    bieffe, please, learn to snip irrelevant material...
    </OT>
    > On 9 Ott, 17:43, harijay <> wrote:

    (snip)
    >> NameError: name 'master' is not defined"


    (snip)

    >> #File runner.py
    >> #!/usr/bin/python
    >> import master
    >> import child
    >>
    >> if __name__=="__main__":
    >> print "RUNNING RUNNER"
    >> m = master.master("hj","oldhj")
    >> s = child.child(m)

    (snip)
    >> #File child.py
    >> class child():
    >> def __init__(self,master):
    >> print "Master name is %s" % master.name
    >> print "Now seeting master name to setnameinchild in child.py "
    >> tmp = master.trash
    >> master.trash = master.name
    >> master.name = "setnameinchild"

    (snip)
    >
    > You need to have an import master in child.py too.


    Nope. The 'child' class is passed a 'master' instance when instanciated.
    You got confused by the bad naming.

    <OP>
    The convention in Python is to name classes in CamelCase. Your classes
    should be named 'Master' and 'Child'.
    </OP>
    Bruno Desthuilliers, Oct 9, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    559
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    579
    Bengt Richter
    Jul 11, 2005
  3. Sean Ross
    Replies:
    3
    Views:
    109
    Aredridel
    Dec 25, 2003
  4. Replies:
    6
    Views:
    101
    Vivek
    Jan 25, 2006
  5. Replies:
    7
    Views:
    142
Loading...

Share This Page