M
M
Maximka1232018-09-01 15:31:34
Python
Maximka123, 2018-09-01 15:31:34

Explain the structure of the code. Why is that?

Good afternoon. I hope you help me. I just can’t understand some points in Python, apparently I’m dumb, so I really count on your help, dear community. The fact is that I cannot understand why when calling class methods, in some cases ( ) brackets are put, but in some cases they are not. An example print(sys.version)at the same time print(time.time( ))The fact is that I was looking for an error in this banal example for 20 minutes, since in my version of the program it was written print(sys.version( ))
Please explain the moments with brackets.
Many thanks in advance to everyone!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
Helow19274, 2018-09-01
@Maximka123

class Class:
    def __init__(self):
        self.value = 1

    def method(self):
        return 'something'

c = Class()
print(c.value)  # 1
print(c.method())  # something

c.value is an attribute of an instance of Class
c.method() is a method (function) of an instance of Class
Attribute as a variable, parentheses are not needed to get its value
A method is a function. Parentheses are needed to call functions
Very little information, but in Russian: https://pythonworld.ru/osnovy/obektno-orientirovan...

0
0xD34F, 2018-09-01
@0xD34F

And where did you get that version is a method? No, seriously, where are you from? Tell me, please.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question