Python Forum

Full Version: how do you define Class in python ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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. ?
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(...)
(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'
Yes, my mistake
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 ?
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()
(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.