Python Forum
Better Understanding Of Object Orientation In Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Better Understanding Of Object Orientation In Python
#4
(Aug-29-2020, 02:43 PM)jefsummers Wrote: Also remember that a class can contain functions (methods) and while a dictionary _can_ do that, it is less common for them to do so.
Less common, but it is exactly how python stores methods for the class. Unless you define the class to use slots, Python classes contain a dictionary named __dict__. This dictionary contains all the class attributes; class variables and methods.

I think looking at what goes in __dict__ is very informative.
class Scope:
    one = 1

    def __init__(self):
        self.two = 2
        
    def three(self):
        self._three = 3

scope = Scope()
scope2 = Scope()
scope2.three()

print('Scope class:', [item for item in Scope.__dict__])
print('scope instance:', [item for item in scope.__dict__])
print('scope2 instance:', [item for item in scope2.__dict__])
Output:
Scope class: ['__module__', 'one', '__init__', 'three', '__dict__', '__weakref__', '__doc__'] scope instance: ['two'] scope2 instance ['two', '_three']
Classes have a __dict__ and so does every instance of the class. The class __dict__ and the instance __dict__ are different. Notice that the class __dict__ knows about the class variables ('one') and methods ('__init__', 'three'). The instance __dict__ only knows about instance variables. scope1 knows about 'two' which is an instance variable set in the __init__ method. scope2 also knows about '_three' which is an instance variable created when the three() method was called. I could also add an instance variable externally
scope.four=4
This would add an instance variable to the instance scope, but would not affect the class or any other instance.
Reply


Messages In This Thread
RE: Better Understanding Of Object Orientation In Python - by deanhystad - Aug-30-2020, 02:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Understanding venv; How do I ensure my python script uses the environment every time? Calab 1 2,434 May-10-2023, 02:13 PM
Last Post: Calab
  New to python/coding Need help on Understanding why this code isn't working. Thanks! mat3372 8 1,913 May-09-2023, 08:47 AM
Last Post: buran
  Understanding Python classes PythonNewbee 3 1,283 Nov-10-2022, 11:07 PM
Last Post: deanhystad
  python update binary object (override delivered Object properties) pierre38 4 1,854 May-19-2022, 07:52 AM
Last Post: pierre38
  Understanding Python super() for classes OmegaRed94 1 1,903 Jun-09-2021, 09:02 AM
Last Post: buran
  fpdf orientation not working properly KatMac 1 3,434 May-02-2021, 10:47 AM
Last Post: Pedroski55
  Understanding Python's Import Engine MysticaL 1 2,242 Feb-07-2020, 11:26 PM
Last Post: snippsat
  Help with understanding a python package pyhill00 4 3,156 Mar-21-2019, 12:42 AM
Last Post: Larz60+
  Understanding if Statements in Python Kathleen 1 2,489 Mar-05-2019, 07:55 PM
Last Post: Yoriz
  Understanding Scoping in Python yksingh1097 5 4,011 Aug-06-2018, 07:42 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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