class ClassName:
# Constructor
def __init__(self, value):
self.varName = value
def functionName(self):
print(self.varName)
class ClassName:
# Constructor
def __init__(self, value):
self.varName = value
# Alternative Constructor
@classmethod
def functionName(cls, varS):
return cls(varS.split("")[N])
varName = ClassName("value")
varName = ClassName.functionName("value")
varName1 = ClassName(parameters)
varName1.functionName()
ClassName.functionName(varName1) # Converted to this above line
print(varName1.varName)
print(varName1.__dir__()) # Returns list of all methods that can be used
print(varName1.__dict__) # Returns dictionary representation of object attributes
print(help(varName1)) # Returns documentation for an object
class ClassName:
varName2 = value2 # Class Variable, Associated with Class
def __init__(self, value):
self.varName = value
self.varName1 = value1 # Instance Variable, Associated with Object
@classmethod
def functionName(cls, value):
cls.varName2 = value # Changes class variable, First argument is instance of Class instead of Object
varName = ClassName()
varName.varName1 = value # Changing Instance variable
varName.varName2 = value # Changing Class variable, Creates a instance variable
varName.functionName(value) # Changing Class variable
print(varName.varName2) # First searched for Instance Variable then Class Variable
class ClassName:
# Getter
@property
def value(self):
return self._value
# Setter
@value.setter
def __init__(self, value):
self._value = value
varName = ClassName(parameters)
print(varName.value)
class ClassName:
# Parent Class
class ClassName1(ClassName):
# Child Class
varName1 = ClassName1()
# Public
class ClassName:
def __init__(self):
self.varName = value
# Private
def __init__(self):
self.__varName = value
# Protected
def __init__(self):
self._varName = value
varName1 = ClassName()
print(varName1.varName) # Access Public Variable
print(varName1._ClassName__varName) # Access Private Variable, Name Mangling
print(varName1._varName) # Access Protected Variable
# Public
class ClassName:
@staticmethod
def functionName():
# Statements
ClassName.functionName(parameters)