Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

@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')
Comment

class method in python

# Python program to demonstrate
# use of a class method and static method.
from datetime import date
  
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
  
    # a class method to create a
    # Person object by birth year.
    @classmethod
    def fromBirthYear(cls, name, year):
        return cls(name, date.today().year - year)
  
    # a static method to check if a
    # Person is adult or not.
    @staticmethod
    def isAdult(age):
        return age > 18
  
person1 = Person('mayank', 21)
person2 = Person.fromBirthYear('mayank', 1996)
  
print(person1.age)
print(person2.age)
  
# print the result
print(Person.isAdult(22))
Comment

PREVIOUS NEXT
Code Example
Python :: validating credit card numbers 
Python :: wordcount pyspark 
Python :: datatime add time in float 
Python :: np.array_equal 
Python :: import folder from another folder python 
Python :: how to divide string in python 
Python :: cannot create group in read-only mode. keras 
Python :: all python statements 
Python :: Run a Flask API from CMD 
Python :: mid-point circle drawing 
Python :: how to extract column from numpy array 
Python :: split df coliumn 
Python :: how to get var value by name godot 
Python :: toolbar pyqt 
Python :: h2o ai python 
Python :: states and capitals us comma separated list 
Python :: python convert datetime to float 
Python :: fonction nombre premier python 
Python :: Query a PSQL Database From Python 
Python :: response time in os 
Python :: update in django orm 
Python :: matplotlib colormap transparent white to black 
Python :: TypeError: __init__(): incompatible constructor arguments. The following argument types are supported: 1. tensorflow.python._pywrap_file_io.BufferedInputStream(arg0: str, arg1: int) 
Python :: qdate to date 
Python :: print with color python 
Python :: pandas df count values less than 0 
Python :: python check if string is in a list 
Python :: python source code 
Python :: aiohttp 
Python :: pydrive download by url 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =