Is there a way to access an attribute outside a class?

Question:

I want to access an attribute outside a class, but even after googling I don’t see any similar solution for this particular problem.

class test():
    def __init__(self) -> None:
        pass
    def testpy(self):
        self.x = 'Hello world'

value = test().testpy.x
print(value)

>> AttributeError: 'function' object has no attribute 'x'
Asked By: Nande Kokoni

||

Answers:

I think you are confused with what an attribute is.

In your code X is a local variable in the member function testpy. X exists only in that function, and is deleted upon function exit.

If you want a member, write self.x = 'Hello world'. This will create and store a data member called x. Additionally, when you write test().testpy().x you are calling the member function testpy() and are then attempting to call .x on the return result, which will obviously break.

Answered By: Mouse

In your code, x is a local variable inside a function, not a class attribute. You can return that variable:

class test():
    def __init__(self) -> None:
        pass
    def testpy(self):
        x = 'Hello world'
        return x

value = test().testpy()
print(value)
Answered By: Ali

Please go through python class tutorial once more.
testpy is a function of class Test, it returns None in the above code. x is an attibute of class Test not function ‘testpy’

class Test():
    def __init__(self):
        pass
    def testpy(self):
        self.x = 'Hello world'

t = Test()
t.testpy()
value = t.x
print(value)
Answered By: Nawal

I think you’re confusing local scope, with class attributes, and global scope.

You can achieve your result in one of three ways:

  • Creating a class attribute to access
  • Using the return keyword to return local values
  • Using the global keyworld to create a globally scoped variable. Highly not recommended
class test:
    def __init__(self) -> None:
        self.x = "Hello world"
        pass

    def testpy(self):
        self.x = "Hello world"
        x = "Hello world"
        global Y
        Y = "Hello world - global"
        return x

object = test()
value_via_objects_attribute = object.x
value_via_objects_method = object.testpy()
value_via_global = Y

print(value_via_objects_attribute)
print(value_via_objects_method)
print(value_via_global)

Answered By: dalekman1234

I think you are confusing your testpy() function with your constructor – the init method. In python, you can initialize a new object by calling the class as if it were a function – ex, "value = test()". When you call this function, the init method gets called, and returns a new object (self). So to declare an attribute on self, in the init function (your constructor), you simply write "self.x = "Hello World".

The full solution to your problem is below.

class test():

  def __init__(self):
     self.x = 'Hello World'
        
value = test()
print(value.x)
Answered By: Raymond Keating
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.