HI
I was having a look into classes and i noticed i saw a lot of things like

class fruitcake(object):
    pass

my question is what is the point of the "object" being in the class arguments as it is a Built In Function and i couldn't work out what putting it there did

Recommended Answers

All 6 Replies

Well basically you have a class called fruitcake, and (object) is the varible you defined. So object may be like this:

object = rectangle(100,45)

that you defined at the beginning of the program. So you use that varible "object" in the class. Here's an example of a class using Shape:

rectangle = Shape(100,45)

def __init__(self,x,y):
        self.x = x
        self.y = y
        description = "This shape has not been defined"
        author = "Nobody has claimed to make this shape"
        def area(self):
            return self.x * self.y
        def perimeter(self):
            return 2 * self.x + 2 * self.y
        def describe(self,text):
            self.description = text
            def authorName(self,text):
                self.author = text
                def scaleSize(self,scale):
                    self.x = self.x * scale
                    self.y = self.y * scale

Hope that helped.

Tondeuse.

Ah i think i have got it!
one last question. When do you need (self) and when is it not needed?

HI
I was having a look into classes and i noticed i saw a lot of things like

class fruitcake(object):
    pass

my question is what is the point of the "object" being in the class arguments as it is a Built In Function and i couldn't work out what putting it there did

Class object is the base class that is at the top of any inheritance tree. It designates the new style Python class (Python24 and higher) having some additional feautures like __slots__ which prevents new variable creation outside the class. __slots__ also replaces the usual __dict__ in the class to give speed improvement.

To show it's methods use:

print dir(object)

Also, do not use 'object' for variable name.

Is there any reason not to inherit object in a class I write?

ich1,

A class that inherits from object is called a "new-style" class. "Old-style" or "classic" classes are likely to be deprecated some time in the future.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.