Answers for "how to call a class method in python"

1

what is a class in python

Classes are blueprint used to construct objects
Posted by: Guest on September-07-2021
0

@methodclass in python

class Date(object):

    def __init__(self, day=0, month=0, year=0):
        self.day = day
        self.month = month
        self.year = year

    @classmethod
    def from_string(cls, date_as_string):
        day, month, year = map(int, date_as_string.split('-'))
        date1 = cls(day, month, year)
        return date1

    @staticmethod
    def is_date_valid(date_as_string):
        day, month, year = map(int, date_as_string.split('-'))
        return day <= 31 and month <= 12 and year <= 3999

date2 = Date.from_string('11-09-2012')
is_date = Date.is_date_valid('11-09-2012')
Posted by: Guest on March-31-2020
0

why should i use classes in python

"Why should I use classes in python?"

Classes are essential in OOP.
A good reason to use classes is to make your code structured and organised. 
Furthermore it is easy to import functions when they are part of a class since you can just import the class.
Classes provide methods that are useful such as the __repr__ method which allows you to represent an object by a string.
Classes allow for access control (encapsulation) by using the underscore character.
Posted by: Guest on July-19-2021
0

python class with function @classmethod

import numpy as np 

class Unknown:
    def __init__(self, p1, p2, p3, p4):
        self.p1 = p1
        self.p2 = p2
        self.p3 = p3
        self.p4 = p4

    def random_func(self, p5):
        '''Function that return an array from a range,
        with specified shape, and name'''
        step1 = np.array(np.arange(self.p1)).reshape((self.p2,self.p3))
        step2 = self.p4
        print(step2)
        print('Printing p5 =>', p5)
        return step1

    @classmethod
    def a_class_method(self, p6):
        print(f'This is a class method: {p6}')
        
        

#PREPARE PARAMS 
# cust_arr = random_func(self.p1, self.p2, self.p3, self.p4)
params = {'p1':12, 'p2':3, 'p3':4, 'p4':'Creating customized array with params'}

#INSTANCIATE
here = Unknown(**params)#16, 4, 4, 'This actually work too!')
here.random_func('This is P5')
Unknown.a_class_method('P6 value is now used !')
Posted by: Guest on August-29-2021

Code answers related to "how to call a class method in python"

Python Answers by Framework

Browse Popular Code Answers by Language