Python Forum
The function of double underscore back and front in a class function name?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The function of double underscore back and front in a class function name?
#9
(Feb-19-2024, 02:21 PM)Pedroski55 Wrote: I read that a class is basically a dictionary. I believe the value of a dictionary key:value pair can be a function.

So in the case of class Car() do we have:

Car = {"color": 'blue', "mileage": 5, "fuel_efficiency": 14.5, "efficiency": calculate_gas_usage() }
Now is classes💪 is lot more that just a dictionary,it store values values from __init__ internally.
Can look at this:
>>> my_car = Car('blue', 8.8, 10.5)
>>> my_car.__dict__
{'color': 'blue', 'fuel_efficiency': 10.5, 'mileage': 8.8}
>>> my_car.__dict__['color']
'blue'
The calculate_gas_usage method is not stored in this dictionary.
Instead, it's a part of the class definition and is accessed through the class's namespace.
When you call my_car.calculate_gas_usage(distance),
Python automatically passes my_car as the first argument to the calculate_gas_usage method.
>>> my_car.calculate_gas_usage(275.5)
26.238095238095237
Reply


Messages In This Thread
RE: The function of double underscore back and front in a class function name? - by snippsat - Feb-19-2024, 03:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  front-end js and back-end python 3lnyn0 0 1,041 Jun-03-2022, 08:51 PM
Last Post: 3lnyn0
  TimeOut a function in a class ? Armandito 1 1,736 Apr-25-2022, 04:51 PM
Last Post: Gribouillis
  Calling a class from a function jc4d 5 1,948 Dec-17-2021, 09:04 PM
Last Post: ndc85430
  Exit function from nested function based on user input Turtle 5 3,057 Oct-10-2021, 12:55 AM
Last Post: Turtle
  a function common to methods of a class Skaperen 7 2,742 Oct-04-2021, 07:07 PM
Last Post: Skaperen
  Tuple generator, and function/class syntax quazirfan 3 4,059 Aug-10-2021, 09:32 AM
Last Post: buran
Question Stopping a parent function from a nested function? wallgraffiti 1 3,753 May-02-2021, 12:21 PM
Last Post: Gribouillis
Question exiting the outer function from the inner function banidjamali 3 3,681 Feb-27-2021, 09:47 AM
Last Post: banidjamali
  Struggling for the past hour to define function and call it back godlyredwall 2 2,296 Oct-29-2020, 02:45 PM
Last Post: deanhystad
  Passing argument from top-level function to embedded function JaneTan 2 2,326 Oct-15-2020, 03:50 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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