Attributes are refering to additional information of an object . Properties are describing the characteristics of an object. Most people use these two words as synonyms.
On w3schools.com :
class Person:
def init(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person(“John”, 36)
p1.age = 40 - this a property or an attribute ? Because Mosh says in the video is an attribute , are the same thing?
Thank you! I’m kinda confused, because of all this different names , a function is called a method or a constructor, attributes or properties, objects, instances …( by the way, what is exactly an instance? )
And thank you again!!! Put another beer on my tab, you’re a real trooper
Cool! Will definitely have that beer! So functions and methods are basically the same. The real difference is that when function is part of a class, then it is called a method. If you just write a basic python script without classes, you would not call it a method, you will call it a function. Only when it is part of a class will you call it a method.
So a constructor is a method of a class.
An instance is when you actually use a class. A class is like a blueprint of how objects should be constructed.
In other words this is a class:
class MyClass:
def __init__:
self.some_attribute
# some code
def some_method():
# some code
But my_class bleow is an instance of MyClass:
my_class = MyClass()
You can have many instances of a MyClass, (eg my_other_class = MyClass() and yet_another_one = MyClass() etc, but you can only have on MyClass class.
Damn, you’re better than medicine! I literally had a headache from this classes, objects,instances and so on, but not anymore, you just made it simpler for me, very grateful for that!!!
It depends on how 2 things. Firstly if the method is correctly indented and secondly if the method has a self param. So in the first example below some_method() is part of the class, in the second it is not.
class MyClass:
def __init__(self):
self.some_attribute
# some code
def some_method(self):
# some code
object = MyClass()
class MyClass:
def __init__(self):
self.some_attribute
# some code
def some_method():
# some code
object = MyClass()
No. Each instance of the class will have its own version of the attribute. If you want to have attributes shared between all instances you need to work with class level attributes which are declared outside the init function (google class level variables). Everything inside init is separate and unique for each instance.
Sorry for this, but I have a lot of questions … the difference between a class method and an instance method is the decorator? and … an object is equal or the same with an object ? because the word instance is giving me a real hard time …
It does not have anything to do with decorators. Decorators are actually a different concept that can apply to methods, functions and classes that are somewhat advanced.
A class and an object (instance) is not the same thing. When you start your program from the command line with the command python my_program.py, your program code is loaded into a specific part of your computer’s main memory. The python interpreter (the program that runs all python programs that we write) knows where this code is loaded. The space in the main memory where my_program.py is loaded into contains everything you have written including all the classes you wrote. This is where you classes “live`”. At this point your program has not started running yet, so it is merely loaded as the “recipe” of what must happen. At this point the “recipe” you wrote (all your code in other words) are merely loaded into memory.
After it is loaded into memory, the Python interpreter starts executing your code line by line. Every time the Python interpreter comes across a variable or parameter etc. in your “recipe” it creates an “instance” of them in another part of the memory, separate and different to where the “recipe” is loaded. This part of the memory is referred to as the Stack.
So, lets say you have a class defined in your code as follows:
class MyClass:
def __init__():
#do_something
the Python interpreter actually does nothing with it. Only when it gets to the line that says:
my_new_class = MyClass()
does it create a new instance of this class on the Stack. In other words, it creates space on the stack (in memory) with a specific address (you can actually see what this address is using print(id(my_new_class)) . This is also referred to as an object. This object is what does all the work. If you have attributes in this class, they will live in this address space that is created for this instance.
If, later in your program you have another call to create a new MyClass variable (instance) lets say
my_newer_class = MyClass() the Python interpreter will go to the main memory where the “recipe” for this class is stored (the same “recipe” it used the first time) and create a new instance in a different part of you rcomputor’s memory of MyClass(), call it my_newer_class and it will have a different memory address. That is why if i change an attribute of one of the instances, it will not have an impact on the other one. You can also check that id(my_new_class) is different to id(my_newer_class) showing that they are two different instances of the same “recipe”, MyClass. It is exactly like baking two different cakes using the same recipe.
Yes. Do not get too worried about all the names. With Python everything on the stack are objects. Strings, integers etc. So my_new_class is an object but this object is also an instance of MyClass. Just like ‘My string’ is an object and an instance of the String class. It is like saying my birthday cake is a Cake object but it is an instance of the Chocolate Cake recipe. My friend may have a muffin object which is an instance of the Muffin recipe. They are all objects but instances of different classes and they live in different spaces created at different times, used for different things and consumed at different times.
After writing the above code (string index out of range), Ones you will print “ value” then the error will appear as an “ IndexError: string index out of range ” . Here, index with name[4] is not in the range, so this error arises because the index value is not present and it is out of range.
To solve this IndexError: string index out of range we need to give the string index in the range so that this error can be resolved. The index starts from 0 and ends with the number of characters in the string.
Example:
name = "Jack"
value = name[2]
print('The character is: ',value)
After writing the above code IndexError: string index out of range this is resolved by giving the string index in the range. Here, the index name[2] is in the range and it will give the output as ” The character is: c ” because the specified index value and the character is in the range.