Teachnique
      CourseRoadmaps
      Login

      OverviewCommentsUser InputNumbersBooleansHistoryHello World ProgramEnvironment SetupSyntaxVariablesData TypesType CastingUnicode SystemLiteralsOperators

      Control FlowBreak StatementContinue StatementPass StatementNested LoopsDecision MakingIf StatementIf-else StatementNested IF StatementMatch-Case StatementLoopsFor LoopsFor-else LoopsWhile Loops

      FunctionsBuilt-in FunctionsDefault ArgumentsKeyword ArgumentsKeyword-Only ArgumentsPositional ArgumentsPositional-Only ArgumentsArbitrary ArgumentsVariable ScopeFunction AnnotationsModules

      StringSlicing StringsModify StringsString ConcatenationString FormattingEscape CharactersString MethodsString Exercises

      ListsList ExercisesAccess List ItemsChange List ItemsAdd List ItemsRemove List ItemsLoop ListsList ComprehensionSort ListsCopy ListsJoin ListsList Methods

      TuplesAccess Tuple ItemsUpdate TuplesUnpack Tuple ItemsLoop TuplesJoin TuplesTuple MethodsTuple Exercises

      SetsAccess Set ItemsAdd Set ItemsRemove Set ItemsLoop SetsJoin SetsCopy SetsSet OperatorsSet MethodsSet Exercises

      DictionariesDictionary ExercisesAccess Dictionary ItemsChange Dictionary ItemsAdd Dictionary ItemsRemove Dictionary ItemsDictionary View ObjectsLoop DictionariesCopy DictionariesNested DictionariesDictionary Methods

      ArraysAccess Array ItemsAdd Array ItemsRemove Array ItemsLoop ArraysCopy ArraysReverse ArraysSort ArraysJoin ArraysArray MethodsArray Exercises

      File HandlingWrite to FileRead FilesRenaming and Deleting FilesDirectoriesFile Methods

      OOP ConceptsDynamic BindingDynamic TypingAbstractionObject and ClassesEncapsulationInterfacesPackagesInner ClassesAnonymous Class and ObjectsSingleton ClassWrapper ClassesEnumsReflectionClass AttributesClass MethodsStatic MethodsConstructorsAccess ModifiersInheritancePolymorphismMethod OverridingMethod Overloading

      Feedback

      Submit request if you have any questions.

      Course
      Class Attributes

      Python Tutorial

      This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.

      Class Attributes

      Every Python class keeps the following built-in attributes and they can be accessed using dot operator like any other attribute −
      • __dict__− Dictionary containing the class's namespace.
      • __doc__ − Class documentation string or none, if undefined.
      • __name__ − Class name.
      • __module__ − Module name in which the class is defined. This attribute is "__main__" in interactive mode.
      • __bases__ − A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list.
      
      For the above class, let us try to access all these attributes
      class Employee:
      def __init__(self, name="Bhavana", age=24):
      self.name = name
      self.age = age
      def displayEmployee(self):
      print ("Name : ", self.name, ", age: ", self.age)
      
      print ("Employee.__doc__:", Employee.__doc__)
      print ("Employee.__name__:", Employee.__name__)
      print ("Employee.__module__:", Employee.__module__)
      print ("Employee.__bases__:", Employee.__bases__)
      print ("Employee.__dict__:", Employee.__dict__ )
      It will produce the following output
      Employee.__doc__: None
      Employee.__name__: Employee
      Employee.__module__: __main__
      Employee.__bases__: (<class 'object'>,)
      Employee.__dict__: {'__module__': '__main__', '__init__': <function Employee.__init__ at 0x0000022F866B8B80>, 'displayEmployee': <function Employee.displayEmployee at 0x0000022F866B9760>, '__dict__': <attribute '__dict__' of 'Employee' objects>, '__weakref__': <attribute '__weakref__' of 'Employee' objects>, '__doc__': None}

      Class Variables

      In the above Employee class example, name and age are instance variables, as their values may be different for each object. A class attribute or variable whose value is shared among all the instances of a in this class. A class attribute represents common attribute of all objects of a class.
      Class attributes are not initialized inside __init__() constructor. They are defined in the class, but outside any method. They can be accessed by name of class in addition to object. In other words, a class attribute available to class as well as its object.

      Example

      Let us add a class variable called empCount in Employee class. For each object declared, the __init__() method is automatically called. This method initializes the instance variables as well as increments the empCount by 1.
      class Employee:
      empCount = 0
      def __init__(self, name, age):
      self.__name = name
      self.__age = age
      Employee.empCount += 1
      print ("Name: ", self.__name, "Age: ", self.__age)
      print ("Employee Number:", Employee.empCount)
      
      e1 = Employee("Bhavana", 24)
      e2 = Employee("Rajesh", 26)
      e3 = Employee("John", 27)

      Output

      We have declared three objects. Every time, the empCount increments by 1.
      Name: Bhavana Age: 24
      Employee Number: 1
      Name: Rajesh Age: 26
      Employee Number: 2
      Name: John Age: 27
      Employee Number: 3

      Practice with Online Editor

      Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
      Remember to save code(Ctrl + S Or Command + S) before run it.