Videos
Hi! I have very less time working with python and now I am going through classes, but until now they look kind of similar to definition of functions. When should I use one and when another? Is there a clear example for it?
Thanks a lot!
you have to use self as the first parameters of a method
in the second case you should use
class MathOperations:
def testAddition (self,x, y):
return x + y
def testMultiplication (self,a, b):
return a * b
and in your code you could do the following
tmp = MathOperations()
print tmp.testAddition(2,3)
if you use the class without instantiating a variable first
print MathOperation.testAddtion(2,3)
it gives you an error "TypeError: unbound method"
if you want to do that you will need the @staticmethod decorator
For example:
class MathsOperations:
@staticmethod
def testAddition (x, y):
return x + y
@staticmethod
def testMultiplication (a, b):
return a * b
then in your code you could use
print MathsOperations.testAddition(2,3)
disclaimer: this is not a just to the point answer, it's more like a piece of advice, even if the answer can be found on the references
IMHO: object oriented programming in Python sucks quite a lot.
The method dispatching is not very straightforward, you need to know about bound/unbound instance/class (and static!) methods; you can have multiple inheritance and need to deal with legacy and new style classes (yours was old style) and know how the MRO works, properties...
In brief: too complex, with lots of things happening under the hood. Let me even say, it is unpythonic, as there are many different ways to achieve the same things.
My advice: use OOP only when it's really useful. Usually this means writing classes that implement well known protocols and integrate seamlessly with the rest of the system. Do not create lots of classes just for the sake of writing object oriented code.
Take a good read to this pages:
- http://docs.python.org/reference/datamodel.html
- http://docs.python.org/tutorial/classes.html
you'll find them quite useful.
If you really want to learn OOP, I'd suggest starting with a more conventional language, like Java. It's not half as fun as Python, but it's more predictable.
They are wrong. But, it's a minor confusion they made and doing video courses which involves speaking and typing can be certainly challenging. No big deal.
When the function belongs to a class, it's a method (a more specialized form of a function). When it's outside of a class it's a function.
How do I know they are wrong?
You use this syntax to create one in Python:
class SomeClass:
@classmethod
def the_method(cls, vars):
....
def instance_method(self, vars):
...
It's not a @classfunction decorator. It's a @classmethod decorator.
See the docs at https://docs.python.org/3/library/functions.html#classmethod
Method is the correct term for a function in a class. Methods and functions are pretty similar to each other. The only difference is that a method is called with an object and has the possibility to modify data of an object. Functions can modify and return data but they dont have an impact on objects.
Edit : Class function and method both mean the same thing although saying class function is not the right way to say it.
There is a function type, but there is no built-in name for it. You can find another reference there under the types module:
>>> import types
>>> def T1():
... pass
...
>>> T1.__class__ is types.FunctionType
True
>>> print(repr(types.FunctionType))
<class 'function'>
So, the only difference here is that function is not a built-in name, unlike names such as int. If you want that name in your namespace for some reason, you can just bind it:
>>> function = type(lambda: 0)
>>> function
<class 'function'>
Think of it this way. Say you create your own metaclass (class of a class), and a subclass of your metaclass (which is itself just a class):
>>> class MyMeta(type): # <-- a custom metaclass, similar to function
pass
>>> print(MyMeta.__name__)
MyMeta
>>> print(__class__)
<class 'type'>
>>> class MyClass(metaclass = MyMeta):
pass
>>> print(MyClass.__class__)
<class 'MyMeta'>
Now, we will delete the identifier MyMeta:
>>> del MyMeta
Can you still get to the metaclass object that was represented by MyMeta although MyMeta has been deleted? Sure:
>>> print(MyClass.__class__)
<class 'MyMeta'>
So there is still a reference to the metaclass object in the MyClass dict.
However, the name MyMeta is now invalid, since it was deleted:
>>> class MyClassTwo(metaclass = MyMeta):
pass
NameError!!
>>> print(MyMeta.__name__)
NameError!!
>>> print(MyMeta)
NameError!!
IMPORTANT: The metaclass name has been deleted, not the metaclass object itself.
Therefore you can still access the metaclass object this way:
>>> class MyClassTwo(metaclass = MyClass.__class__):
pass
So it is with the function name (which is itself kind of like a built-in metaclass; i.e., it is the class, or type, of function objects)- by default, the name function doesn't exist in an interactive Python session. But the object does still exist. You can access it this way:
>>> def f(): pass
>>> f.__class__.__class__
<class 'type'>
And if you like, you can even go ahead and assign the name function to the function <class 'type'> object (although there's not much reason to do that):
>>> function = f.__class__
>>> print(function)
<class 'function'>
>>> print(function.__class__)
<class 'type'>