Class in another tab.

edited April 2017 in Python Mode

I have a class called "Square".

When I put the class in the main file, it works.

But when I put the class into another tab called Square, says: "NameError: global name 'Square' is not defined".

If I write "import Square", says: TypeError: 'module' object is not callable.

What's the problem?

Thank you.

Tagged:

Answers

  • name the tab differently

    sketches should not hold the same name as the class

  • I tried, but got the same error...

  • Circle is your class? Or is it in a package? Why are you using import if it is in another tab? When Processing executes, it brings all the code from different tabs together. Keep in mind tabs are physically stored as files. You have to make sure the file is a pde file. If it has another extension, like ".java", then it will behave differently.

    Kf

  • edited April 2017 Answer ✓
    • In Python Mode, the main 1st tab is a ".pyde" extension file.
    • However, when we add extra tabs, those are of extension ".py" instead.
    • And each of those ".py" tabs is treated as a Python module.
    • And Python Mode compiles those as ".class" files as well.
    • But don't worry, we can still access the whole Processing's API from ".py" tabs. :)>-
    • So in order to import a whole ".py" tab, we can use: from Name_of_the_Tab import *
    • Below's a simple sample on how to do multi-tab in Python Mode: :bz

    "TabImportTest.pyde":

    # https://forum.Processing.org/two/discussion/22043/class-in-another-tab#Item_4
    # GoToLoop (2017-Apr-17)
    
    from Square import *
    
    def setup():
        square = Square(random(10))
        print square, square.area()
        exit()
    

    "Square.py":

    class Square:
        def __init__(self, x): self.x = x
        def area(self): return sq(self.x)
        def __str__(self): return `self.x`
    
  • "Circle" was a typo.

    My class is "Square", the tab is "Square".

    GoToLoop gave the right answer: "from Square import *"

    My mistake was write only "import Square" .

    Thank you guys.

  • edited April 2017

    Of course you can be more specific and type in: from Square import Square. ;)

  • Oh, right, thanks for the clarification.

  • edited April 2017

    Another variation w/ import only and w/o from. :P
    However, we've gotta use name_of_the_tab.property_name instead of just property_name: :-<

    # https://forum.Processing.org/two/discussion/22043/class-in-another-tab#Item_8
    # GoToLoop (2017-Apr-17)
    
    import Square
    
    def setup():
        global square
        square = Square.Square(random(10))
        print square, square.area()
        exit()
    
  • edited April 2017

    Oh, nice, the more you know...

    Thank you.

Sign In or Register to comment.