Python Forum
Printing the variable from defined function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Printing the variable from defined function
#8
Adding attributes is something you can do with most python objects.

In python everything is an object. Each object is an instance of a class. The class defines what methods (functions) the object can execute, and the __init__() method assigns attributes. But the __init__() method is just a convenience. When you assign a value to an attribute in the __init__() method, you are adding a key/value pair to a dictionary, __dict__. You can add attributes to the object at any time, from anywhere, and they are added to the __dict__. That is all I did in my example. I added a "name" attribute to a DataFrame object's __dict__. The DataFrame class is completely unaware of the new attribute.

Not all objects support adding attributes. Some python classes use __slots__ instead of __dict__. Slots objects use less memory and they are faster than objects that use __dict__, but they are less flexible because their attributes are set by the class (the attribute names, not the values). And some python classes that use __dict__ disable the mechanism that lets you add attributes to an object. But these are in the minority, and most python objects let you add whatever attributes you want whenever you want.
class MostlyEmptyClass:
    def __init__(self):
        self.a = 1

x = MostlyEmptyClass()
print(x.__dict__)
x.b = 2
print(x.__dict__)
Output:
{'a': 1} {'a': 1, 'b': 2}
When adding attributes this way it is wise to first print the dir() to verify you are not stomping all over an existing instance variable OR METHOD. Reading the documentation does not tell you everything about a class.
Output:
>>> dir(x) ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'b']
Reply


Messages In This Thread
RE: Printing the variable from defined function - by deanhystad - Sep-03-2023, 03:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Variable not defined even though it is CoderMerv 3 392 Mar-28-2024, 02:13 PM
Last Post: Larz60+
  Variable for the value element in the index function?? Learner1 8 747 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 660 Nov-23-2023, 02:53 PM
Last Post: rob101
  Function parameter not writing to variable Karp 5 1,044 Aug-07-2023, 05:58 PM
Last Post: Karp
  Getting NameError for a function that is defined JonWayn 2 1,166 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 3,022 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  How to print the output of a defined function bshoushtarian 4 1,370 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  Retrieve variable from function labgoggles 2 1,092 Jul-01-2022, 07:23 PM
Last Post: labgoggles
  User-defined function to reset variables? Mark17 3 1,717 May-25-2022, 07:22 PM
Last Post: Gribouillis
  [variable] is not defined error arises despite variable being defined TheTypicalDoge 4 2,243 Apr-05-2022, 04:55 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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