2

I saw this thread a few days ago, and I understand(after reading the answers) why the OP was getting the behavior in question, but I do not understand what is happening with the dot operator being used on the "add" function: print add().a. a is an element of class test, and add creates an instance of test, but how is a being accessed right on the function?

2 Answers 2

6

In that post, the function add() returns a test object. That object has an attribute a, so it can be accessed using the . operator.

In other words, add().a accesses the attribute a of the result of the function call to add, not of the function object itself (that would be add.a).

Sign up to request clarification or add additional context in comments.

1 Comment

@JakeSellers: I'm not sure I understand. .a is applied to whatever add() returns, regardless of how many other things it does.
2

add() this creates an object, it is not assigned to a name (perhaps that's the confusion), then .a acesses the member of the (unnamed) add() object.

print simply does what it needs to do.

It is almost the same as doing this:

foo = add()
print foo.a
del(foo)

Comments

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.