python class parameters code example

Example 1: python classes

class Box(object): #(object) ending not required
  def __init__(self, color, width, height): # Constructor: These parameters will be used upon class calling(Except self)
    self.color = color # self refers to global variables that can only be used throughout the class
    self.width = width
    self.height = height
    self.area = width * height
  def writeAboutBox(self): # self is almost always required for a function in a class, unless you don't want to use any of the global class variables
    print(f"I'm a box with the area of {self.area}, and a color of: {self.color}!")

greenSquare = Box("green", 10, 10) #Creates new square
greenSquare.writeAboutBox() # Calls writeAboutBox function of greenSquare object

Example 2: call instance class python

# define class
class example:
# define __call__ function
   def __call__(self):
       print("It worked!")
# create instance
g = example()
# when attempting to call instance of class it will call the __class method
g()
# prints It worked!

Example 3: python class

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

    def speak(self):
        print("Hi I'm ", self.name, 'and I am', self.age, 'Years Old')

JUB0T = Dog('JUB0T', 55)
Friend = Dog('Doge', 10)
JUB0T.speak()
Friend.speak()

Example 4: class methods parameters python

class Foo          (object):
    # ^class name  #^ inherits from object

    bar = "Bar" #Class attribute.

    def __init__(self):
        #        #^ The first variable is the class instance in methods.  
        #        #  This is called "self" by convention, but could be any name you want.
        #^ double underscore (dunder) methods are usually special.  This one 
        #  gets called immediately after a new instance is created.

        self.variable = "Foo" #instance attribute.
        print self.variable, self.bar  #<---self.bar references class attribute
        self.bar = " Bar is now Baz"   #<---self.bar is now an instance attribute
        print self.variable, self.bar  

    def method(self, arg1, arg2):
        #This method has arguments.  You would call it like this:  instance.method(1, 2)
        print "in method (args):", arg1, arg2
        print "in method (attributes):", self.variable, self.bar


a = Foo() # this calls __init__ (indirectly), output:
                 # Foo bar
                 # Foo  Bar is now Baz
print a.variable # Foo
a.variable = "bar"
a.method(1, 2) # output:
               # in method (args): 1 2
               # in method (attributes): bar  Bar is now Baz
Foo.method(a, 1, 2) #<--- Same as a.method(1, 2).  This makes it a little more explicit what the argument "self" actually is.

class Bar(object):
    def __init__(self, arg):
        self.arg = arg
        self.Foo = Foo()

b = Bar(a)
b.arg.variable = "something"
print a.variable # something
print b.Foo.variable # Foo

Example 5: class python

class A:        # define your class A
.....

class B:         # define your class B
.....

class C(A, B):   # subclass of A and B
  
obj = C() #to create instance
# issubclass(sub, sup) boolean function returns true if the given 
# subclass sub is indeed a subclass of the superclass sup

# isinstance(obj, Class) boolean function returns true if obj is an 
# instance of class Class or is an instance of a subclass of Class

Tags:

Lua Example