Python Forum
How to define a method in a class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to define a method in a class
#3
You define the method with self parameter but when you call it, you don't pass it. Only the positional/keyword arguments.

class MyClass:
    def method_1(self):
        print('One')
  
    def method_2(self, number):
        doubled = number * 2
        print('Doubled:' , doubled)
        return doubled

cl = MyClass()
cl.method_1()
doubled = cl.method_2(10)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Messages In This Thread
How to define a method in a class - by 1885 - Oct-24-2017, 12:57 PM
RE: How to define a method in a class - by Lux - Oct-29-2017, 01:40 AM
RE: How to define a method in a class - by wavic - Oct-29-2017, 02:00 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] define entry via class on a loop drSlump 9 3,420 Oct-27-2021, 05:01 PM
Last Post: deanhystad
  [PyQt] I get a name Name Error: when calling a method inside a class radoo 2 2,371 Jun-11-2020, 05:02 PM
Last Post: radoo
  [Tkinter] Issue with calling a Class/Method in GUI File Fre3k 3 3,018 Mar-08-2020, 12:35 PM
Last Post: Fre3k
  [Kivy] How do I reference a method in another class? Exsul1 3 4,270 Mar-02-2020, 07:32 PM
Last Post: Exsul1
  [Tkinter] Call a class method from another (Tk GUI) class? Marbelous 3 6,191 Jan-15-2020, 06:55 PM
Last Post: Marbelous
  [Tkinter] Class with text widget method AeranicusCascadia 3 7,785 Nov-14-2017, 11:33 PM
Last Post: AeranicusCascadia
  [PyQt] How to put class method into new module? California 0 2,919 Jan-18-2017, 04:05 PM
Last Post: California

Forum Jump:

User Panel Messages

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