Skip to content Skip to sidebar Skip to footer

Why Can't I Access A Variable From Another Class Python

When I run hello printer I get this error AttributeError: type object 'Setup' has no attribute 'hello' please help! hello printer from rpg import * print Setup.hello rpg class

Solution 1:

Either make setup method static or insert self argument.

classSetup(object):
    @staticmethoddefsetup():
        hello = 5print hello
Setup.setup()

As its a static method, you don't have to initialize a class. or

class Setup(object):
    def setup(self):
        hello = 5
        print hello
s = Setup()
s.setup()

In this case you will have to create an object of Setup class as you will be accessing the class method.

Solution 2:

You define hello in the function setup so it does not belong to the class namespace. To access a class variable you need to define it in the class, not in a function:

classSetup(object):
    hello =5

Post a Comment for "Why Can't I Access A Variable From Another Class Python"