2 Replies - 216 Views - Last Post: 28 January 2013 - 08:51 PM Rate Topic: -----

#1 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Calling Class Error

Posted 28 January 2013 - 04:20 PM

Not really an error and yet an error. It doesn't seem to "break" anything or halt the program, but it appeared shortly after the defining of my UI class and prompts as soon as the program is executed. I think it may be the way I imported the graphics module or something but I'm not exactly sure what.

The error that seemingly does nothing but loudly complain.
Traceback (most recent call last):
  File "C:\Python\workbench\2D_astrophysics.py", line 33, in <module>
    class func(object):
  File "C:\Python\workbench\2D_astrophysics.py", line 53, in func
    def set_attractor(self, val = self.G):
NameError: name 'self' is not defined




What code it's complaining about.
import math
from graphics import*

class UI(object):

    def makeWin(self):
        self.win = GraphWin("Vectors", 512, 512)
        self.win.setBackground("black")

    def closeWin(self):
        self.win.close()
        
class func(object):

    def __init__(self):
        self.G = 6.67384*(10**-11)

    def set_foo(self, foo):
        self.foo = foo

    def get_foo(self):
        return self.foo

    def set_bar(self, bar):
        self.bar = bar

    def get_bar(self):
        return self.bar

    ...
    ...
    ...

    def set_attractor(self, val = self.G):
        self.G_const = val

    def get_attractor(self):
        return self.G_const
    ...
    ...
    ...



[edit:]
Actually, it does halt the second class from being operated upon.

>>> x = UI()
>>> y = func()

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    y = func()
NameError: name 'func' is not defined
>>> 


This post has been edited by BloodyInari: 28 January 2013 - 04:21 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Calling Class Error

#2 Nallo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 162
  • View blog
  • Posts: 248
  • Joined: 19-July 09

Re: Calling Class Error

Posted 28 January 2013 - 05:08 PM

Python complains correctly. The reason is: when Python runs into a class statement it first builds a dictionary of all the methods. It looks at the arguments and finds a default value self.G. But self refers to an instance to the class which does not yet exist. So Python complains.

You can see it with the following toy example. Nothing but a class definition but when you run it python complains:
class foo(object):
    def __init__(self):
        self.bar = "baz"
    def sef_bar(self, bar=self.bar):
        self.bar = bar



There are two ways around this:

The old some_function(bar=None) trick:
class func(object):
    def __init__(self):
        self.G = 123
    def set_attractor(self, val=None):
        if val is None:
            val = self.G
        self.G_const = val

f = func()
f.set_attractor()
print f.G_const



or you could make G a class constant:
class func(object):
    G = 123
    def set_attractor(self, val=G):
        self.G_const = val

f = func()
f.set_attractor()
print f.G_const


In which case func.G/self.G is the same for all instances (so make sure not to alter self.G)
Was This Post Helpful? 2
  • +
  • -

#3 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Re: Calling Class Error

Posted 28 January 2013 - 08:51 PM

View PostNallo, on 28 January 2013 - 06:08 PM, said:

Python complains correctly. The reason is: when Python runs into a class statement it first builds a dictionary of all the methods. It looks at the arguments and finds a default value self.G. But self refers to an instance to the class which does not yet exist. So Python complains.

The old some_function(bar=None) trick:
class func(object):
    def __init__(self):
        self.G = 123
    def set_attractor(self, val=None):
        if val is None:
            val = self.G
        self.G_const = val

f = func()
f.set_attractor()
print f.G_const



I see, makes sense...
well problem fixed. Thanks for the help. :rockon:

This post has been edited by BloodyInari: 28 January 2013 - 09:40 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1