as you said to run above code, i did that in two way
one as @io_io you said foo = A
and second as @zedshaw shown in EX40 like calling a class like function foo = A()
i have changed your code a bit
class A(object):
def __init__(self):
self.K = "bjgrft"
print("self = ",self)
foo = A
print("foo = ",foo)
print("A = ",A)
Q = "whatever"
print("Q = ",Q)
#print("foo.K = ",foo.K)
foo.K = Q
print("foo = ",foo)
print("foo.K = ",foo.K)
print("Q = ",Q)
print("foo.K == 'whatever'",foo.K == "whatever")
output of this is
if i don’t comment line 12 which is [print("foo.K = ",foo.K)] then i got error which is
AttributeError: type object 'A' has no attribute 'K'
and by second way calling class like function
class A(object):
def __init__(self):
self.K = "bjgrft"
print("self = ",self)
foo = A()
print("foo = ",foo)
print("A = ",A)
Q = "whatever"
print("Q = ",Q)
print("foo.K = ",foo.K)
foo.K = Q
print("foo = ",foo)
print("foo.K = ",foo.K)
print("Q = ",Q)
print("foo.K == 'whatever'",foo.K == "whatever")
i got output like
can you explain difference between two ways
in second way i have not put comment on line 12 as before
and the difference i’m seeing in both way is
- self is printed in second way, not in first way
- getting an memory location of object foo in second way
- in first way i can’t fetch value of K which is on line 12, in second way i can fetch the K
and also can you tell me difference between instance and object
because zed says in section objects are like class of ex40 that
When you instantiate a class what you get is called an object.
and in word drill of ex41 zed says
object = Two meanings: the most basic type of thing, and any instance of some thing.
instance = What you get when you tell Python to create a class.