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
#2
When I try this I also get the NoneType Error


class HardwareMaintenance:
    #info_dict = dict()
    def __init__(self,info_dict = dict() ):
        self.info_dict = info_dict
    def hardware_handler(ConnectHandler, ip=None, self=None):
            self.info_dict[ip] = []
for this line:
self.info_dict[ip] = []


can someone guide me?


thanks
Reply
#3
The arguments of hardware_handler are in the wrong order. Self must be the first argument
class HardwareMaintenance:

    def __init__(self, info_dict = None ):
        self.info_dict = dict() if info_dict is None else info_dict

    def hardware_handler(self, ConnectHandler, ip=None):
            self.info_dict[ip] = []
billykid999 likes this post
Reply
#4
(May-02-2023, 06:58 AM)Gribouillis Wrote: The arguments of hardware_handler are in the wrong order. Self must be the first argument
class HardwareMaintenance:

    def __init__(self, info_dict = None ):
        self.info_dict = dict() if info_dict is None else info_dict

    def hardware_handler(self, ConnectHandler, ip=None):
            self.info_dict[ip] = []


Thank you this cleared up just about everything for me, and I have a much better understanding where i went wrong. I'm going to stick with the old code, as this one will force me to do multiple inheritance which i don't need right now. However your suggestion worked. Thanks.
Reply
#5
(May-02-2023, 11:24 AM)billykid999 Wrote: as this one will force me to do multiple inheritance
I don't see how it would force you to do multiple inheritance. Nobody is ever forced to do that.
Reply
#6
(May-02-2023, 04:09 AM)billykid999 Wrote: When I write my below code everything works fine:
Note that this/original snippet and all the rest have one important difference - in the original snippet info_dict is class attribute and being mutable dict it is shared among all instances of the class, i.e. if you update the dict in one instance, it is also reflected in all instances.

In all the rest it's an instance attribute.
billykid999 likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(May-02-2023, 12:31 PM)buran Wrote: In all the rest it's an instance attribute.
It is not true in the version where the instance's info_dict is initialized in the constructor's arguments list
class HardwareMaintenance:

    def __init__(self,info_dict = dict() ):
        self.info_dict = info_dict
This is not good because the default info_dict is created once and shared between all the instances.
Reply
#8
(May-02-2023, 01:30 PM)Gribouillis Wrote: It is not true in the version where the instance's info_dict is initialized in the constructor's arguments list

Technically it's still an instance attribute as I said, not class attribute. Then there is the gotcha of the mutable default argument
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
(May-02-2023, 11:51 AM)Gribouillis Wrote:
(May-02-2023, 11:24 AM)billykid999 Wrote: as this one will force me to do multiple inheritance
I don't see how it would force you to do multiple inheritance. Nobody is ever forced to do that.

I may be inheriting from my individual classes in the furture, which is why I wrote them in classes to begin with - that and the code looks cleaner, and it's easier to track which functions belong to which class if I add more functions inside each class, which may be a need if i decide to try to automate cloud devices and I need different functions to handle it. The multiple inheritance will be useful at that point if i inherit from two different classes to use to functions/commands. It isn't forced by declaring inf_dict as an attribute, I mis-spoke. When i declare info_dict right under the class and not as an attribute instance it makes the code look a little uglier, but it also takes away from that I may not even need classes at that point ~ declaring them as instance attributes gives me a reason for making it a class other than the notes mentioned above. However I recognized i was calling each classes functions (i.e HardwareMaintenance) without instantiating the class in a main calling class. i'd have to refactor the code for, from what I'm seeing little to no improvement int performance. By the way the tips helped me a lot, I was going nuts trying to see where I was going wrong, because if I may decide to add attributes to the classes and inherit from multiple classes for ad-hoc issues, which was my original intent of making info_dict as an instance (__init__) attribute.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  question about __repr__ in a class akbarza 4 617 Jan-12-2024, 11:22 AM
Last Post: DeaD_EyE
Question __init__ of Child Class zero_fX0 4 1,733 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,324 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  class, attribute and method Frankduc 9 2,488 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,352 Dec-30-2021, 04:01 PM
Last Post: mtldvl
  Not including a constructor __init__ in the class definition... bytecrunch 3 11,942 Sep-02-2021, 04:40 AM
Last Post: deanhystad
  Python generics: How to infer generic type from class attribute? Thoufak 0 2,830 Apr-25-2021, 09:31 AM
Last Post: Thoufak
  AttributeError class object has no attribute list object scttfnch 5 3,472 Feb-24-2021, 10:03 PM
Last Post: scttfnch
  newbie question....importing a created class ridgerunnersjw 5 2,679 Oct-01-2020, 07:59 PM
Last Post: ridgerunnersjw
  "can't set attribute" on class DreamingInsanity 2 10,893 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