Terminology
Mental model:
- A variable stored in an instance or class is called an attribute.
- A function stored in an instance or class is called a method.
According to Python's glossary:
attribute: A value associated with an object which is referenced by name using dotted expressions. For example, if an object o has an attribute a it would be referenced as o.a
method: A function which is defined inside a class body. If called as an attribute of an instance of that class, the method will get the instance object as its first argument (which is usually called
self). See function and nested scope.
Examples
Terminology applied to actual code:
a = 10 # variable
def f(b): # function
return b ** 2
class C:
c = 20 # class attribute
def __init__(self, d): # "dunder" method
self.d = d # instance attribute
def show(self): # method
print(self.c, self.d)
e = C(30)
e.g = 40 # another instance attribute
Answer from Raymond Hettinger on Stack OverflowVideos
Python class method versus instance method: What’s the difference?
In Python, a class method is a method that is invoked with the class as the context. This is often called a static method in other programming languages. An instance method, on the other hand, is invoked with an instance as the context.
What happens if both instance attribute and class attribute are defined?
In that case, the instance namespace takes precedence over the class namespace. If there is an attribute with the same name in both, the instance namespace will be checked first and its value returned.
What is a Python namespace?
A Python namespace is a mapping from names to objects, with the property that there is zero relation between names in different namespaces. Namespaces are usually implemented as Python dictionaries, although this is abstracted away.
Terminology
Mental model:
- A variable stored in an instance or class is called an attribute.
- A function stored in an instance or class is called a method.
According to Python's glossary:
attribute: A value associated with an object which is referenced by name using dotted expressions. For example, if an object o has an attribute a it would be referenced as o.a
method: A function which is defined inside a class body. If called as an attribute of an instance of that class, the method will get the instance object as its first argument (which is usually called
self). See function and nested scope.
Examples
Terminology applied to actual code:
a = 10 # variable
def f(b): # function
return b ** 2
class C:
c = 20 # class attribute
def __init__(self, d): # "dunder" method
self.d = d # instance attribute
def show(self): # method
print(self.c, self.d)
e = C(30)
e.g = 40 # another instance attribute
A method is an attribute, but not all attributes are methods. For example, if we have the class
class MyClass(object):
class_name = 'My Class'
def my_method(self):
print('Hello World!')
This class has two attributes, class_name and my_method. But only my_method is a method. Methods are functions that belong to your object. There are additional hidden attributes present on all classes, but this is what your exercise is likely talking about.
Class attributes are same for all instances of class whereas instance attributes is particular for each instance. Instance attributes are for data specific for each instance and class attributes supposed to be used by all instances of the class.
"Instance methods" is a specific class attributes which accept instance of class as first attribute and suppose to manipulate with that instance.
"Class methods" is a methods defined within class which accept class as first attribute not instance(that's why the are class methods).
You can easily see class attributes by accessing A.__dict__:
class A:
class_attribute = 10
def class_method(self):
self.instance_attribute = 'I am instance attribute'
print A.__dict__
#{'__module__': '__main__', 'class_method': <function class_method at 0x10978ab18>, 'class_attribute': 10, '__doc__': None}
And instance attributes as A().__dict__:
a = A()
a.class_method()
print a.__dict__
# {'instance_attribute': 'I am instance attribute'}
Some useful links from official python documentation and SO, which i hope won't confuse you more and give clearer understanding...
- Python Classes
- Dive into python, 5.8. Introducing Class Attributes
- Definition of python getattr and setattr
- What are Class methods in Python for?
- Difference between Class and Instance methods
To answer this question, you need to first think about how python looks up attributes. I'll assume you know what an instance is. As a refresher. . .
class SomeClass(object):
class_attribute = 'This -- Defined at the class level.'
def __init__(self):
self.instance_attribute = 'This -- Defined on an instance.'
def method(self):
pass
instance = SomeClass()
Class attributes are defined at the class level. Instance attributes are defined at the instance level (usually via self.xyz = ...). Instance attributes can also be monkey patched onto the instance:
instance.another_instance_attribute = '...'
Notice that class attributes can be looked up on an instance if an instance attribute doesn't "shadow" it.
print(instance.class_attribute)
Now for methods. Methods are another animal entirely.
SomeClass.method
is definitely an attribute on the class.
instance.method
is a bit trickier to classify. Actually, when python sees that statement, it actually executes the following:
SomeClass.method.__get__(instance, SomeClass)
Weird. So what is happening here is that functions are descriptors. Since they have a __get__ method, it gets called and the return value (in this case) is a new function that knows what self is. The normal terms are "instance method" and "bound method". I suppose some might consider it an instance attribute, but, I'm not sure that's exactly correct (even though you access it via the instance)
This link here can explain more about class attributes/methods/parameters.
However, I do understand how complicated these concepts are, so I will answer your question (although in the future, try to ask a more specific question!).
In example one:
init (the initializer) and print_time are both class attributes. When you initialize the clock variable and pass in the parameter '5:30', it accesses the init function and hits the self.time = time line of code. Since time is accessed using a dot notation, time is an INSTANCE attribute (specific to the individual object).
When you call self.print_time(), the time there is a local variable specific to the function call, therefore the instance attribute is not changed. That is why when you print self.time it is still 5:30.
In example two:
In this case, the init and print_time functions are both class attributes (similar to the example above). The initialization of the clock object is the same as above. However, when it calls print_time, time is a the parameter '10:30', and therefore when we just print time (notice we did not use any dot notation), it prints only the local variable of '10:30'.
In example three:
init and print_time are both class attributes, same as the above two examples. When you initialize the boston_clock object, it is similar to both example one and two. Then you assign the name paris_clock to the object boston_block (notice that paris_clock and boston_clock are just names pointing to the same object, like how I could have two names). Therefore when we execute the line of code paris_clock.time = '10:30', the INSTANCE attribute of this single object is changed to '10:30'. However, since boston_clock is pointing to the same object as paris_clock, boston_clock's time attribute is also '10:30'.
Attributes are the variables within a class or instance. In something like this the variable hello is an attribute of the class Hi.
class Hi:
hello = "Hello World!"
Methods are functions within the class, so for something like this, function greet is a method of the class Hi.
class Hi:
def greet(self):
pass
Parameters are input(s) that go into a method. So the string, "Hello World!" is a parameter of the method say in the class Hi.
class Hi:
def say(self, saying):
print(saying)
Hi().say("Hello World!")
There's a nice question on the Software Engineering StackExchange site about OOPL. Explaining OOP Concepts to a non technical person.