Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python inheritance

class Base():
    """ My base class """

    __nb_instances = 0

    def __init__(self):
        Base.__nb_instances += 1
        self.id = Base.__nb_instances

class User(Base):
    """ My User class """

    def __init__(self):
        super().__init__()
        self.id += 99

u = User()
print(u.id)
Comment

Python Inheritance

class Student(Person):
  def __init__(self, fname, lname, year):
    super().__init__(fname, lname)
    self.graduationyear = year
 

x = Student("Michael", "Smith", 2020)
Comment

python inheritance

class Coordinates:
    def __init__(self, x, y):
        self.__x = x #variables that start with double _ are private and only can be accessed by method
        self.__y = y
    def x(self):
        return self.__x
    def y(self):
        return self.__y
    def position(self):
        return (self.__x, self.__y)

class Square(Coordinates):
    def __init__(self, x, y, w, h):
        super().__init__(x, y)
        self.__w = w
        self.__h = h
    def w(self):
        return self.__w
    def h(self):
        return self.__h
    def area(self):
        return self.__w * self.__h

class Cube(Square):
    def __init__(self, x, y, w, h, d):
        super().__init__(x,y,w,h)
        self.__d = d
    def d(self):
        return self.__d
    def area(self): #overwrites square area function
        return 2 * (self.__w * self.__h + self.__d * self.__h + self.__w * self.__d)
    def volume(self):
        return self.__w * self.__h * self.__d
Comment

Python Inheritance

class BaseClass:
  Body of base class
class DerivedClass(BaseClass):
  Body of derived class
Comment

python inherit

# Python inheritance is super simple

class BaseClass: ...  
class SubClass(BaseClass): ... # inherits all values from BaseClass

# See, real simple
Comment

PREVIOUS NEXT
Code Example
Python :: python3 delete file 
Python :: dataframe coulmn to list 
Python :: python : search file for string 
Python :: read header of csv file python 
Python :: python check if variable has value 
Python :: python dataframe find no of true 
Python :: pandas explode 
Python :: python sort dictionary case insensitive 
Python :: python discord bot embed 
Python :: matplotlib cheat sheet 
Python :: Python NumPy asfarray Function Example List to float type array 
Python :: python http post file 
Python :: django connexion session time 
Python :: create period pandas 
Python :: adding in python 
Python :: speed typing test python 
Python :: select first row of every group pandas 
Python :: This code is supposed to display "2 + 2 = 4" on the screen, but there is an error. Find the error in the code and fix it, so that the output is correct. 
Python :: how to get the user argent in django 
Python :: show only lower diagonal in sns pairplot 
Python :: slug 
Python :: Randome Word generator from consonant, vowel and specific string 
Python :: python copy formula ghseets 
Python :: python event start from file funcion 
Shell :: kill localhost 3000 ubuntu 
Shell :: check supervisord status 
Shell :: Failed to start docker.service: Unit docker.service is masked 
Shell :: react router v5 install 
Shell :: stop a process running on a port ubuntu 
Shell :: install redis on mac 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =