Python Forum
how do you define Class in python ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how do you define Class in python ?
#1
how do you define Class in python ?


class MyClass;

OR

class MyClass();

which one is correct ?

It seems I have seen both varieties ... hence confused . Could you please differentiate and explain the correct version. ?
Reply
#2
parenthesis are used when you are overloading another class;
example:
class MyGui(wx.Frame):
    def __init__(parent, id, title, pos, size, style):
        wx.Frame.__init__(self, parent, id, title, pos, size, style)
This is done when you want all of the functionality of the first class, but want to modify at least one method in the original

With out parenthesis if no overloading (developing a new class)
example:
class MyGui:
    def __init__(self):
        frame = wx.Frame(...)
Reply
#3
(Mar-03-2018, 07:02 PM)Larz60+ Wrote: parenthesis are used when you are overloading another class;
overloading is not the right word or expatiation Larz60+.
It's Inheritance so class MyGui inherits from wx.Frame,no overloading is going on.

volcano Wrote:how do you define Class in python ?
# Python 3
class Foo:
    pass

# The same in Python 2,called new-style class
class Foo(object):
    pass
So in Python 3 there is no ().
() only come in play when it's Inheritance.

Class OtherBreed inherits all from parent class Tiger.
Therefor will class OtherBreed have class attribute(also called class variable) species.
>>> class Tiger:
...     species = 'mammal'
...     
>>> class OtherBreed(Tiger):
...     pass
... 
>>> bengal_tiger = OtherBreed()
>>> bengal_tiger.species
'mammal'
Reply
#4
Yes, my mistake
Reply
#5
Thanks for the reply.

you said
Quote: So in Python 3 there is no ().
() only come in play when it's Inheritance.

Please look at this code below. There is no inheritance here.

class Mario():
    
    def move(self):
        print('I am moving!')



bm = Mario()
bm.move()
Output:
--------
I am moving!

I am using Python interpreter 3.4

I am getting confused now ..does class Mario() : and class Mario: is same thing in Python 3 ?

comments please. what I'm missing ?
Reply
#6
It's the same definition. The parenthesis is not needed in this case. However, when you create an instance you will need it.

class Mario:
     
    def move(self):
        print('I am moving!')

bm = Mario()
bm.move()
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
(Mar-04-2018, 05:27 AM)volcano Wrote: I am getting confused now ..does class Mario() : and class Mario: is same thing in Python 3 ?
Yes. Empty parenthesis is the same as not having parentheses at all.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to use a variable in Python (2.x) to define decimal part? MDRI 4 2,325 May-07-2021, 12:39 AM
Last Post: MDRI
  How to define a variable in Python that points to or is a reference to a list member JeffDelmas 4 2,643 Feb-28-2021, 10:38 PM
Last Post: JeffDelmas
  How to create and define in one line a 2D list of class objects in Python T2ioTD 1 2,030 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  dynamically define class's michavardy 1 2,080 Feb-26-2019, 04:20 PM
Last Post: buran
  Converting c++ class to python class panoss 12 11,816 Jul-23-2017, 01:16 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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