# Python Processing refer to an object from main script

**URL:** <https://discourse.processing.org/t/python-processing-refer-to-an-object-from-main-script/13272>\
**Category:** Processing.py\
**Created:** [August 9, 2019, 10:00am UTC](https://discourse.processing.org/t/python-processing-refer-to-an-object-from-main-script/13272 "2019-08-09T10:00:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [August 9, 2019, 10:00am UTC](https://discourse.processing.org/t/python-processing-refer-to-an-object-from-main-script/13272/1 "2019-08-09T10:00:55Z")

</div>

OK, I just started learning Python & I never liked how people had the classes on same page everything else went on. So I placed them on another page, but sadly I don’t know how to actually get to them. I liked one of the tutorials I was doing and tried to make it more. I tried using import sounded good but still running into errors.

here is sketch, the main page that I don’t want the classes on

```auto
def setup():
    size(600,600)
    
def draw():
    background(0)
    
def fight(unit1,unit2):
    round = 0;
    if round == 0:
        unit2.health -= unit1.attack
        if unit2.health > 0:
            round = 1;
        else:
            unit2.is_alive = false
    if round == 1:
        unit1.health -= unit2.attack
        return True;
        if unit1.health > 0:
            round = 0;
        else:
            unit1.is_alive = false
            return False;
        
if __name__ == ' __main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing

    chuck = Warrior()
    bruce = Warrior()
    carl = Knight()
    dave = Warrior()
    mark = Warrior()

    assert fight(chuck, bruce) == True
    assert fight(dave, carl) == False
    assert chuck.is_alive == True
    assert bruce.is_alive == False
    assert carl.is_alive == True
    assert dave.is_alive == False
    assert fight(carl, mark) == False
    assert carl.is_alive == False

```

for knight & warrior I know they short for now but I plan to add more later

here is knight.py

```auto
class Knight(Warrior):
    attack = 7

```

here Warrior

```auto
class Warrior:
    health = 50
    is_alive = true;
    attack = 5;

```

---

<div class="post-metadata">

**Author:** ![humayung](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/humayung/32/6089_2.png) [@humayung](https://discourse.processing.org/u/humayung)\
**Post date:** [August 9, 2019, 10:38am UTC](https://discourse.processing.org/t/python-processing-refer-to-an-object-from-main-script/13272/2 "2019-08-09T10:38:51Z")

</div>

There is an old topic from old processing forum

> **[Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/22043/class-in-another-tab)**
>
> Processing is an electronic sketchbook, a language and a worldwide community. This is its forum.

`from Name_of_the_Tab import *` (@GoToLoop answer)

you need to save the tab first time to get this work

 ![Screenshot%20from%202019-08-09%2017-37-16](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c79a9913a066eefc929bf3e691f46d08c678e6de.png)

---

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [August 9, 2019, 10:40am UTC](https://discourse.processing.org/t/python-processing-refer-to-an-object-from-main-script/13272/3 "2019-08-09T10:40:07Z")

</div>

do I need to import on every tab?

---

<div class="post-metadata">

**Author:** ![humayung](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/humayung/32/6089_2.png) [@humayung](https://discourse.processing.org/u/humayung)\
**Post date:** [August 9, 2019, 10:41am UTC](https://discourse.processing.org/t/python-processing-refer-to-an-object-from-main-script/13272/4 "2019-08-09T10:41:25Z")

</div>

yes. you treated them like modules

> <https://github.com/jdf/processing.py/issues/45>

---

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [August 9, 2019, 10:47am UTC](https://discourse.processing.org/t/python-processing-refer-to-an-object-from-main-script/13272/5 "2019-08-09T10:47:32Z")

</div>

I tried import here Knight

```auto
from Warrior import *
class Knight(Warrior):
    attack = 7

```

as you can see I placed important in it so he can get Warrior class, but it still says Warrior undefined, guessing it can’t find it

---

<div class="post-metadata">

**Author:** ![humayung](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/humayung/32/6089_2.png) [@humayung](https://discourse.processing.org/u/humayung)\
**Post date:** [August 9, 2019, 10:55am UTC](https://discourse.processing.org/t/python-processing-refer-to-an-object-from-main-script/13272/6 "2019-08-09T10:55:09Z")

</div>

> [@ctrembla](#):
>
> class Warrior: health = 50 is\_alive = true; attack = 5;

🤔 for me it just work fine. Try to delete _~$py.class_ things in the folder and rerun? got it work?  
 ![Screenshot%20from%202019-08-09%2017-54-02](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/591bb16eb286e900b20d0f483e0be2278e4c8304.png)

---

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [August 9, 2019, 10:57am UTC](https://discourse.processing.org/t/python-processing-refer-to-an-object-from-main-script/13272/7 "2019-08-09T10:57:46Z")

</div>

TY, it worked after I closed and opened it now I need to try and solve the next part after I understand what it wants me to do. TYVM

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 9, 2019, 1:23pm UTC](https://discourse.processing.org/t/python-processing-refer-to-an-object-from-main-script/13272/8 "2019-08-09T13:23:08Z")

</div>

IMO, the Python Mode’s dev made a bad decision on how to deal w/ multi-tabbing in Processing! 😲

In Java Mode, when we create a new tab, by default that tab is of extension “.pde”.

And when we run the sketch, all “.pde” files are concatenated as 1 “.java” file. ➕

So there’s no `import` among “.pde” files, b/c they’re treated as 1 file after all. 😌

However, does that mean there’s no way to have some kinda module system under Java Mode? 📦

Actually there is! When we create a new tab, we have the undocumented option to name it w/ a “.java” extension. 😮

Let’s say we’d name the new tab “Warrior”. That’ll result in a “Warrior.pde” named file. 📁

However, if we name it instead as “Warrior.java”, that’ll result exactly in a “Warrior.java” file. ⚔

Therefore, it’s not a “.pde” file anymore; and now `import` is necessary in order for it to be available for the “.pde” files, just like any other Java library! ☕

So I wonder why the Python Mode’s dev decided not to follow such simple & flexible scheme… 😞

Instead, he’s forcing all additional tabs to be treated as a Python module rather than letting us decide which 1s we want so! 😖

It should just default all extra tabs to be of extension “.pyde”, and concatenate them all as 1 file when we run the sketch, just like Java Mode. 🙄

Actually, both modes should offer 2 ways to create a new tab: a regular 1 and a module 1. 💡
