0

I should access the variable in a method in the class.

Because I did some data cutting in the method, I need the data of which data cutting is already done.

But I can access an instance variable that is only defined in the "__init__" method.

I give an example because of making clear my question. Let's see the below code.

class test:
    def __init__(self,a,b):
        self.a = a
        self.b = b
    def t(self,c,d):
        self.c = c
        self.d = d

FirstTest = test(3,4)
print(FirstTest.a)

SecondTest = test(3,4)
print(SecondTest.t(30,40).c)

I need "c" and "d", but I can not access of these. I only access to "a" and "b" If I try to access "c" and "d", below error is coming up.

---> 13 print(SecondTest.t(30,40).c)

AttributeError: 'NoneType' object has no attribute 'c'

Is there no code I can access the instance variable which is not defined in "__init__"?

3
  • 2
    The t method doesn't return the instance, it returns None. You can access c and d if you do so on that actual instance after calling that method. Commented Jun 25, 2022 at 16:29
  • 2
    The t method doesn't return anything, so it's meaningless to try to access the c attribute of it. You need to do the method call as a separate statement from printing the attribute. Commented Jun 25, 2022 at 16:29
  • 2
    The standard convention in Python is to capitalize the class names, and use all lowercase for the variables referring to instances. class Test: ..., first_test = Test(3, 4), etc. Commented Jun 25, 2022 at 16:34

3 Answers 3

3

The t() method doesn't return the instance. You can access the attribute after calling the method by referring to the variable.

SecondTest.t(30, 40)
print(SecondTest.c)
Sign up to request clarification or add additional context in comments.

Comments

1

The function t() returns nothing, or None. You can do as the previous answer suggests and return self, but this isn't normally a good idea. If you just want c, use:

SecondTest = test(3,4)
SecondTest.t(30,40)
print(SecondTest.c)

Comments

1

You're not returning anything from t(), so None is returned. If you want to chain method calls with an attribute access, you'll need to return self so that the return value of t is the object whose attribute you want to access (though, as jonrsharpe points out, mutating an instance and returning it can be hard to reason about, so be sure that you want to support this kind of interface before actually implementing it):

def t(self,c,d):
    self.c = c
    self.d = d
    return self

This outputs:

3
30

2 Comments

But note that the convention in Python is for methods that mutate the instance to return None, rather than this kind of "fluent interface".
@BrokenBenchmark Thanks for the comment but it's not suitable in my case because I should return another value for 'curve_fit'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.