Python classes. brackets or not? (Found an answer. See below)

Hello @zedshaw .
I am now at exercise 41.
While studying phrase drills and searched google for the parts I did not understand at once I found examples of classes with no brackets, with empty brackets and with ‘object’ inside brackets.
I did this code as example wich will work with any of these creations of the class

I am curios to know when I should use these three options?
Or when not to use them?

class MyClass: # works
# or
class MyClass(): # works
class MyClass(object): # works
    soon = 50

    def __init__(self, real_name, username, age):
        self. real_name = real_name
        self. username = username
        self.age = age

    def start_study_python(self, wish, years_ago):
        return wish, self.age  - years_ago, 'years ago'         
    def then(self):
        return "I was",self.age ,'then'

me = MyClass('Ulf', 'Ulfen', 48)
print(me.start_study_python("I wish I have started to study Python", 24))
print("In two years I will be", me.soon)
me.age = 28
print("I wish I was", me.age, "instead")

My username is: Ulfen
(‘I wish I have started to study Python’, 24, ‘years ago’)
(‘I was’, 24, ‘then’)
In two years I am 50
I wish I was 28 instead

Found an answer :slight_smile:

When I looked at the video for ex42 there was a good explanation for my question.
MyClass:" is the same as "MyClass(object):

At Stack Overflow I also found this:
Python class inherits object
Scroll down a little bit for the "Python 3.x story:"
There it is explained MyClass(): is also the same as MyClass(object):

1 Like

Yessssssss, so Python 3 you can do just class MyClass: and be done with it. However, after conferring with many people in Python 3 it seems that people prefer the explicit nature of class MyClass(object): so that’s what I use in the book.