# Classes - Attribute or properties

**URL:** <https://forum.codewithmosh.com/t/classes-attribute-or-properties/8364>\
**Category:** Python\
**Created:** [November 7, 2021, 6:34pm UTC](https://forum.codewithmosh.com/t/classes-attribute-or-properties/8364 "2021-11-07T18:34:19Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lt.Smith](https://avatars.discourse-cdn.com/v4/letter/l/3bc359/32.png) [@Lt.Smith](https://forum.codewithmosh.com/u/Lt.Smith)\
**Post date:** [November 7, 2021, 6:34pm UTC](https://forum.codewithmosh.com/t/classes-attribute-or-properties/8364/1 "2021-11-07T18:34:19Z")

</div>

I found this :

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](http://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?

print(p1.age)

---

<div class="post-metadata">

**Author:** ![Paul](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Paul](https://forum.codewithmosh.com/u/Paul)\
**Post date:** [November 7, 2021, 6:39pm UTC](https://forum.codewithmosh.com/t/classes-attribute-or-properties/8364/2 "2021-11-07T18:39:15Z")

</div>

You can use either property or attribute. They are the same.

---

<div class="post-metadata">

**Author:** ![Lt.Smith](https://avatars.discourse-cdn.com/v4/letter/l/3bc359/32.png) [@Lt.Smith](https://forum.codewithmosh.com/u/Lt.Smith)\
**Post date:** [November 7, 2021, 7:00pm UTC](https://forum.codewithmosh.com/t/classes-attribute-or-properties/8364/3 "2021-11-07T19:00:04Z")

</div>

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 😉

---

<div class="post-metadata">

**Author:** ![Paul](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Paul](https://forum.codewithmosh.com/u/Paul)\
**Post date:** [November 7, 2021, 7:08pm UTC](https://forum.codewithmosh.com/t/classes-attribute-or-properties/8364/4 "2021-11-07T19:08:17Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Lt.Smith](https://avatars.discourse-cdn.com/v4/letter/l/3bc359/32.png) [@Lt.Smith](https://forum.codewithmosh.com/u/Lt.Smith)\
**Post date:** [November 7, 2021, 7:17pm UTC](https://forum.codewithmosh.com/t/classes-attribute-or-properties/8364/5 "2021-11-07T19:17:30Z")

</div>

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!!! 😃

---

<div class="post-metadata">

**Author:** ![Lt.Smith](https://avatars.discourse-cdn.com/v4/letter/l/3bc359/32.png) [@Lt.Smith](https://forum.codewithmosh.com/u/Lt.Smith)\
**Post date:** [November 10, 2021, 9:49am UTC](https://forum.codewithmosh.com/t/classes-attribute-or-properties/8364/6 "2021-11-10T09:49:40Z")

</div>

class MyClass:

def **init** :  
self.some\_attribute  
# some code

def some\_method():  
# some code

object = MyClass()

Hi again! 😃

Both functions(methods) belong to the object of the class, or just the second one beneath the **init** method(function) ?

---

<div class="post-metadata">

**Author:** ![Paul](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Paul](https://forum.codewithmosh.com/u/Paul)\
**Post date:** [November 10, 2021, 3:54pm UTC](https://forum.codewithmosh.com/t/classes-attribute-or-properties/8364/7 "2021-11-10T15:54:56Z")

</div>

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.

```auto
class MyClass:

    def __init__ (self):
        self.some_attribute
        # some code
    
    def some_method(self):
        # some code

object = MyClass()

```

```auto
class MyClass:

    def __init__ (self):
        self.some_attribute
        # some code

def some_method():
    # some code

object = MyClass()

```

---

<div class="post-metadata">

**Author:** ![Lt.Smith](https://avatars.discourse-cdn.com/v4/letter/l/3bc359/32.png) [@Lt.Smith](https://forum.codewithmosh.com/u/Lt.Smith)\
**Post date:** [November 10, 2021, 8:21pm UTC](https://forum.codewithmosh.com/t/classes-attribute-or-properties/8364/8 "2021-11-10T20:21:12Z")

</div>

Thank you very much !!! Put another beer on that tab 😃

---

<div class="post-metadata">

**Author:** ![Lt.Smith](https://avatars.discourse-cdn.com/v4/letter/l/3bc359/32.png) [@Lt.Smith](https://forum.codewithmosh.com/u/Lt.Smith)\
**Post date:** [November 10, 2021, 8:35pm UTC](https://forum.codewithmosh.com/t/classes-attribute-or-properties/8364/9 "2021-11-10T20:35:52Z")

</div>

Another question : in the second example, will the data that is stored in the class attributes, will be shared among the instance attributes ?

---

<div class="post-metadata">

**Author:** ![Paul](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Paul](https://forum.codewithmosh.com/u/Paul)\
**Post date:** [November 11, 2021, 7:16am UTC](https://forum.codewithmosh.com/t/classes-attribute-or-properties/8364/10 "2021-11-11T07:16:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Lt.Smith](https://avatars.discourse-cdn.com/v4/letter/l/3bc359/32.png) [@Lt.Smith](https://forum.codewithmosh.com/u/Lt.Smith)\
**Post date:** [November 11, 2021, 2:59pm UTC](https://forum.codewithmosh.com/t/classes-attribute-or-properties/8364/11 "2021-11-11T14:59:45Z")

</div>

I had a feeling that that was the case, but I said better to ask. Thank you again @Paul !!!

---

<div class="post-metadata">

**Author:** ![Lt.Smith](https://avatars.discourse-cdn.com/v4/letter/l/3bc359/32.png) [@Lt.Smith](https://forum.codewithmosh.com/u/Lt.Smith)\
**Post date:** [November 11, 2021, 4:19pm UTC](https://forum.codewithmosh.com/t/classes-attribute-or-properties/8364/12 "2021-11-11T16:19:26Z")

</div>

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 …

---

<div class="post-metadata">

**Author:** ![Paul](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Paul](https://forum.codewithmosh.com/u/Paul)\
**Post date:** [November 11, 2021, 5:53pm UTC](https://forum.codewithmosh.com/t/classes-attribute-or-properties/8364/13 "2021-11-11T17:53:38Z")

</div>

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:

```auto
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.

Hope this helps a bit more.

---

<div class="post-metadata">

**Author:** ![Lt.Smith](https://avatars.discourse-cdn.com/v4/letter/l/3bc359/32.png) [@Lt.Smith](https://forum.codewithmosh.com/u/Lt.Smith)\
**Post date:** [November 11, 2021, 6:49pm UTC](https://forum.codewithmosh.com/t/classes-attribute-or-properties/8364/14 "2021-11-11T18:49:54Z")

</div>

'does it create a new _instance_ of this class on the Stack… This is also referred to as an _object_ ’

‘If, later in your program you have another call to create a new `MyClass` variable (instance)’

This is why I get confused… **instance** is used in the same phrase as object and also it could be a variable…

---

<div class="post-metadata">

**Author:** ![Paul](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Paul](https://forum.codewithmosh.com/u/Paul)\
**Post date:** [November 12, 2021, 4:58am UTC](https://forum.codewithmosh.com/t/classes-attribute-or-properties/8364/15 "2021-11-12T04:58:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![drasticcode12](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@drasticcode12](https://forum.codewithmosh.com/u/drasticcode12)\
**Post date:** [December 3, 2021, 8:25pm UTC](https://forum.codewithmosh.com/t/classes-attribute-or-properties/8364/16 "2021-12-03T20:25:17Z")

</div>

[Python error: String index out of range python](https://drasticcode.com/string-index-out-range-python/)  
What is the “string index out of range” error, why it occurs, the solution to remove the string index out of range error

---

<div class="post-metadata">

**Author:** ![Lt.Smith](https://avatars.discourse-cdn.com/v4/letter/l/3bc359/32.png) [@Lt.Smith](https://forum.codewithmosh.com/u/Lt.Smith)\
**Post date:** [December 6, 2021, 10:05am UTC](https://forum.codewithmosh.com/t/classes-attribute-or-properties/8364/17 "2021-12-06T10:05:59Z")

</div>

```auto
name = "Jack"
value = name[4]
print(value)

```

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:**

```auto
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.
