Python how Object and class attributs work how they Works

Python how Object and class attributs work how they Works

lets define an empty class called Dog:

Aucun texte alternatif pour cette image

Classes contain characteristics called?Attributes. We make a distinction between?instance attributes?and?class attributes.

Instance Attributes?are unique to each object, (an instance is another name for an object). Here, any?Dog?object we create will be able to store its name and age. We can change either attribute of either dog, without affecting any other dog objects we’ve created.

Aucun texte alternatif pour cette image

This?__init__?is called the?initializer. It is automatically called when we instantiate the class. It’s job is to make sure the class has any attributes it needs. It’s sometimes also used to make sure that the object is in a valid state when it’s instantiated, like making sure the user didn’t enter a negative age for the dog.

We have to include the?self?parameter so that our initializer has a reference to the new object being instantiated.

Class Attributes?are unique to each class. Each instance of the class will have this attribute. It’s sometimes used to specify a defualt value that all objects should have after they’ve been instantiated. Here, our class attribute is species

Aucun texte alternatif pour cette image

Class attributes?are the variables defined directly in the class that are shared by all objects of the class.

Instance attributes?are attributes or properties attached to an instance of a class. Instance attributes are defined in the constructor.

The following table lists the difference between class attribute and instance attribute :

Aucun texte alternatif pour cette image

__dict__

Dictionary

Dictionaries are used to store data values in key:value pairs.

A dictionary is a collection which is ordered*, changeable and do not allow duplicates.

要查看或添加评论,请登录

Jaafar Fares的更多文章

  • ?? Unveiling Staffy: My Stellar Graduation Project as a Software Engineering Graduate! ??

    ?? Unveiling Staffy: My Stellar Graduation Project as a Software Engineering Graduate! ??

    As I proudly take the stage as a recent Software Engineering graduate, I'm thrilled to share a major milestone in my…

  • ?? 10 Best Free APIs to Enhance Your Apps ??

    ?? 10 Best Free APIs to Enhance Your Apps ??

    API's play a crucial role in empowering Devolopers in their journeys. These effective equipment offer a gateway to get…

  • STEM

    STEM

    what is STEM: STEM is an approach to learning and development that integrates the areas of science, technology…

  • what happens when you type https://www.google.com

    what happens when you type https://www.google.com

    Every day you open up your browser and navigate to your favorite websites — whether it be social media, news, or…

  • Internet of Things

    Internet of Things

    The internet of things or (IoT) : is a system of interrelated computing devices, mechanical and digital machines…

  • the concept of recursion

    the concept of recursion

    Recursion definition: Recursion is a process in which a function calls itself as a subroutine. This allows the function…

  • In Python Everything is an Object

    In Python Everything is an Object

    in Python everything is an object : >>> Classes are objects, >>>instances of classes are objects, >>>modules are…

  • dynamic and static libraries small peack into them

    dynamic and static libraries small peack into them

    Static Library : A static library is a programming concept in which shared libraries with special functionalities…

  • integers are stored in memory using two’s complement

    integers are stored in memory using two’s complement

    Integers are whole numbers which will be stored in computer using 4 bytes (32 bit) of memory. for example : Binary…

  • Worst abuse of the C preprocessor 1986

    Worst abuse of the C preprocessor 1986

    What’s IOCCC? The International Obfuscated C Code Contest is an annual programming contest with a obfuscated C code. In…

社区洞察