Python Forum
Initiating an attribute in a class __init__: question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Initiating an attribute in a class __init__: question
#1
When I write my below code everything works fine:
class HardwareMaintenance:
    info_dict = dict()
    def __init__(self):
        pass
    def hardware_handler(ConnectHandler, ip=None, self=None):
            #info_dict = HardwareMaintenance.info_dict
            info_dict = HardwareMaintenance.info_dict
            info_dict[ip] = []

            if ConnectHandler.device_type == "cisco_ios":
I would like to pass info_dict into the __init__ method of the class, however when i reference it inside the hardware_handler function using this method it return a Nonetype:


class HardwareMaintenance:

    def __init__(self):
        self.info_dict = dict()
    def hardware_handler(ConnectHandler, ip=None, self=None):
            #info_dict = self.info_dict
            info_dict = self.info_dict
            info_dict[ip] = []
Is there a way where I can initialize info_dict through the __init__ method and call it in my function? Down the line I may inherit the HardwareMaintenance class, and so I would like to have this option of an auto generate dictionary when i do inheritance.
Reply


Messages In This Thread
Initiating an attribute in a class __init__: question - by billykid999 - May-02-2023, 04:09 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  question about __repr__ in a class akbarza 4 815 Jan-12-2024, 11:22 AM
Last Post: DeaD_EyE
Question __init__ of Child Class zero_fX0 4 2,096 Mar-22-2023, 05:23 PM
Last Post: deanhystad
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,429 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  class, attribute and method Frankduc 9 2,764 Feb-27-2022, 09:07 PM
Last Post: deanhystad
  Python class doesn't invoke setter during __init__, not sure if's not supposed to? mtldvl 2 3,551 Dec-30-2021, 04:01 PM
Last Post: mtldvl
  Not including a constructor __init__ in the class definition... bytecrunch 3 12,778 Sep-02-2021, 04:40 AM
Last Post: deanhystad
  Python generics: How to infer generic type from class attribute? Thoufak 0 2,989 Apr-25-2021, 09:31 AM
Last Post: Thoufak
  AttributeError class object has no attribute list object scttfnch 5 3,714 Feb-24-2021, 10:03 PM
Last Post: scttfnch
  newbie question....importing a created class ridgerunnersjw 5 2,848 Oct-01-2020, 07:59 PM
Last Post: ridgerunnersjw
  "can't set attribute" on class DreamingInsanity 2 11,177 Aug-22-2020, 07:57 PM
Last Post: DreamingInsanity

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020