hi all. I know that when you create a class, you put all arguements for that class in a __init__ functions, but what about for actaully making classes? confused? this is what I mean:

class Aclass(Object):
insert code here...

what is the purpose of 'Object'?

Recommended Answers

All 5 Replies

You can replace object with other classes, this is called inheritance.

For example, we have a class called Shape,
We know that:
-All shapes have a name

so lets write that code

class Shape(object):
    def __init__(self,name):
        self.name = name

circle = Shape("circle")

But what if we wanted a class of a Rectangle?
Well we know that:
-A rectangle is just a special Shape
-So therefore has all the properties a shape has
-With a length and width

So lets code that

#We Inherit the Shape class
class Rect(Shape):
    def __init__(self,name,height,width):
        #But we still have to call the __init__ method
        Shape.__init__(name)
        self.height = height
        self.width = width

rect = Rect("Rectangle",10,4)

#has the variables from the Shape class
print rect.name
#OUTPUT
#Rectangle

Hope that helps :)

Here is a class you can look,with some basic function of a class.
Paulthom12345 has a good eksample how a class can inheritance from other class.

Just little fix to Paulthom12345 code.
Shape.__init__(name)
change to
Shape.__init__( self, name )

class SimpleClass(object):
  '''Doc string info about class'''
  
  # Class attributes as you see this is variables
  # They the belong to a class therefor name 'attributes' 
  x = 5
  y = 10
  s = x + y
  
  def __init__(self, string):    
     '''
     * Self is the class glue,and also transport data between methods
     * When you make_object everything in this method get run because of __init__ 
     '''
     self.string = string
     print 'In the constructor'
     print 'Value from argument -->', string
     print ''     
      
  def SayHello(self, string):
    '''This is a function,but in a class it is called a method'''
    print 'Hi, ' + string + '.'
    
  def data_get(self):
    '''Recive data from __init__'''
    print 'Data from __init__ -->', self.string  #Try without self          
   

# Run one and one line understand whats happens    
make_object = SimpleClass('I am an argument')  # Class instance
#make_object_new = SimpleClass("argument from new object")  # New class instance 

#make_object.SayHello('This is Python and i like it')
#make_object_new.SayHello('Object_new like python to')

#make_object.data_get()

#print make_object.x
#print make_object.s
#print make_object_new.y

#print make_object.__doc__
#print help(SimpleClass)

hi all. I know that when you create a class, you put all arguements for that class in a __init__ functions, but what about for actaully making classes? confused? this is what I mean:

class Aclass(Object):
insert code here...

what is the purpose of 'Object'?

This is the newer class writing style and it inherits the built-in 'object' (note that Python is case sensitive!). 'object' contains some rudimentary things for the class.

Use:
print(dir(object))
to look at those.

Here is one of the differences between old and new style class:

class Oldstyle: 
    def __init__(self): 
        self.a = 1 
        self.b = 2 
        self.c = 3 
        print(self.__dict__)  # {'a': 1, 'c': 3, 'b': 2}        
        
class Newstyle(object):
    """
    __slots__ prevents new variable creation outside the class
    and improves the speed by replacing the usual class __dict__
    """
    # 'register' the variables allowed 
    __slots__ = ["a", "b", "c"] 
    def __init__(self): 
        self.a = 1 
        self.b = 2 
        self.c = 3 
        #print(self.__dict__)  # will give error


oldobj = Oldstyle() 
oldobj.d = 4 # no error 

newobj = Newstyle() 
newobj.d = 4 # gives error, since d is not 'registered'

thanks all. I kind of understand it now.

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.