-1

I just wanna create a class definition with a static field with a name. A file called exercises.py contains:

First error:

FAIL: test_00_packages (__main__.Ex00)
Traceback (most recent call last):
File "ex00.py", line 55, in test_00_packages
self.assertTrue("Exercise00" in globals()) 
AssertionError: False is not true

Later:

class Exercise00:
    def __init__(self, STUDENT_NAME):
        self.STUDENT_NAME = 'Name Name'

But if I try to print Exercise00.STUDENT_NAME I just get NameError: name 'Exercise00' is not defined

But I guess I defined it?!

Here the complete error:

ERROR: test_01_static_field (__main__.Ex00)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "ex00.py", line 60, in test_01_static_field
    print("[I] Name: " + Exercise00.STUDENT_NAME)
NameError: name 'Exercise00' is not defined

----------------------------------------------------------------------

My task is to create a class called Exercise00 with a static field STUDENT_NAME.

The line in ex00.py is:

def test_00_packages(self):
    self.assertTrue("Exercise00" in globals())
  • 4
    Please create an [mcve] – Sayse Nov 08 '19 at 11:26
  • 1
    We need to see the code where you try to do the print, as well as the full traceback (people commonly misremember/misreport errors), and also explain whether this is in the same file or not. – Karl Knechtel Nov 08 '19 at 11:27
  • It's an external file, which tries to print STUDENT_NAME – Lunar Cultist Nov 08 '19 at 11:29
  • 2
    Well for one, `STUDENT_NAME` is an instance field so you'd need an instance of the object. but you still need to create a [mcve] – Sayse Nov 08 '19 at 11:30
  • 2
    It doesn’t solve the first error you’re running into, but see https://stackoverflow.com/questions/68645/are-static-class-variables-possible-in-python for how to create a static field (class attribute) instead of an instance attribute like you have now. – Ry- Nov 08 '19 at 11:31
  • We need to see more of what is in `ex00.py`. I think we need to see what is imported in the first few lines of the file. – quamrana Nov 08 '19 at 12:08
  • the file is imported by from folder.exercises import Exercise00 – Lunar Cultist Nov 08 '19 at 12:13
  • Well, I don't understand how this `import` can work without error, and yet `self.assertTrue("Exercise00" in globals())` fails. – quamrana Nov 08 '19 at 12:18

2 Answers2

2

Two problems:

  1. The test class is in a separate file exercises.py; you need to import the relevant functionality from that file (from exercises import Exercise00) before the module contents are visible from ex00.py.

  2. Once you fix that, you will get a different error. Like the name of the test says, you are supposed to be looking for a static field, i.e. something that belongs to the class itself. This code attaches STUDENT_NAME to instances of Exercise00.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • Yes, I took that into account. In ex00.py, you need to have the import statement. Otherwise, ex00.py cannot use contents from exercise.py. – Karl Knechtel Nov 08 '19 at 11:39
2

I suppose you need to define STUDENT_NAME as a class-level field, not as an instance level attribute:

    class Exercise00:
        STUDENT_NAME = 'Name Name'

You can notice in the error message that the test calls class level field Exercise00.STUDENT_NAME:

print("[I] Name: " + Exercise00.STUDENT_NAME)

And you also need to import class Exercise00 in the test module:

from exercises import Exercise00

As soon as you add the import statement to the file with the test ex00.py, the class name string appears in globals() and the test passes.

Max Voitko
  • 1,542
  • 1
  • 17
  • 32