Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python override inherited method class model constructor

class Parent(object):
    def __init__(self, a, b):
        print 'a', a
        print 'b', b

class Child(Parent):
    def __init__(self, c, d, *args, **kwargs):
        print 'c', c
        print 'd', d
        super(Child, self).__init__(*args, **kwargs)

test = Child(1,2,3,4)
Comment

python override inherited method class model constructor

class Bar(object):
   def __init__(self, arg1=None, arg2=None, argN=None):
       print arg1, arg2, argN

class Foo(Bar):
    def __init__(self, my_new_arg=None, **kwds):
       super(Foo, self).__init__(**kwds)
       self.new_arg = my_new_arg
       print my_new_arg

f = Foo(my_new_arg='x', arg2='y')
Comment

python override inherited method data model constructor

class Parent:
    def __init__(self, common, data, **kwargs):
        super().__init__(**kwargs)
        self.common = common
        self.data = data

    @classmethod
    def from_file(cls, filename, **kwargs):
        # If the caller provided a data argument,
        # ignore it and use the data from the file instead.
        kwargs['data'] = load_data(filename)
        return cls(**kwargs)

    @classmethod
    def from_directory(cls, data_dir, **kwargs):
        return [cls.from_file(data_file, **kwargs)
                for data_file in get_data_files(data_dir)]
        

class ChildA(Parent):
    def __init__(self, specific, **kwargs):
        super().__init__(**kwargs)
        self.specific = specific
Comment

PREVIOUS NEXT
Code Example
Python :: # find all text files in directory or any type of files in directory 
Python :: How to run a method before/after all class function calls with arguments passed? 
Python :: cast set 
Python :: if not isinstance multiple values 
Python :: gensim prepare corpus 
Python :: first index of an integer less than a value 
Python :: Fatal Python error: Cannot recover from stack overflow. 
Python :: Double all numbers using a map() and Lamda Function 
Python :: tuple python !g 
Python :: ccacxc 
Python :: XML to table form in Excel 
Python :: List change after copy Python 
Python :: fetch inbox mail python 
Python :: python flask many to many relation db 
Python :: how to end if else statement in python 
Python :: how to show type of a variable 
Python :: Flask - how do I combine Flask-WTF and Flask-SQLAlchemy to edit db models 
Python :: get command line variables python 
Python :: python go back one using abspath 
Python :: ring for loop 
Python :: update specific field in index in elastic using python 
Python :: loop over dict python looking for match in list 
Python :: matplotlib plot dpi - change format to retina instead of svg 
Python :: set change order python 
Python :: global variable not accessible withing thread 
Python :: How to check whether a nested hash element exists in python 
Python :: long type python 
Python :: scrollable dataframe 
Python :: python multilevel list comprehension 
Python :: eager tensor to numpy 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =