python - why does some functions need () but some don't? -


my apologies if stupid question but

if have

x=3+5j 

then x.imag give imaginary part x.conjugate() give complex conjugate. why need put () call conjugate function?

i thinking whether x can thought struct in c++

imag public attribute of x, conjugate member function. way of thinking correct? apologise if makes no sense. admittedly, knowledge of c++ , python both quite limited.

x not struct, object of type complex (although helps think of objects structs methods bound them).

>>> type(x) <class 'complex'> >>> x = 3+5j >>> type(x) <class 'complex'> 

x.imag not function, it's attribute of object x float object. attribute not have called parentheses () in order retrieve value. value 5 in example. can test if it's function calling callable on it. float objects not callable.

>>> x.imag 5.0 >>> type(x.imag) <class 'float'> >>> callable(x.imag) false 

x.conjugate is function, since callable(x.conjucate) returns true. call function putting parentheses () after function parameters inside parentheses. when called, returns new object of type complex, in turn has method conjugate can called.

>>> x.conjugate <built-in method conjugate of complex object @ 0x00000000034d2c90> >>> callable(x.conjugate) true >>> x.conjugate() (3-5j) >>> type(x.conjugate()) <class 'complex'> >>> x.conjugate().conjugate() (3+5j) 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -