Class and instance attributes

Instance = class(arguments)

Attributes and Methods in class:

Instance Attribute

Class Attribute

Differences Between Class and Instance Attributes

What are the advantages and drawbacks of each of them

__dict__

#!/usr/bin/python3
class Person:
"""Defines Person class"""
#Class Variable
class_variable = "Class variable content"
def __init__(self, name, last_name):
"""Constructor method __init__"""
# Instance properties
self.name = name
self.last_name = last_name
#str method definition
def __str__(self):
"""Defines string to return"""
str = self.name + self.last_name
return(str)
#instance creation
person1 = Person ("Kurt", "Cobain")
#dir print
print("Person __dict__:", Person.__dict__)
print("")
print("person1 __dict__:", person1.__dict__)
Person __dict__: {'__module__': '__main__', '__doc__': 'Defines Person class', 'class_variable': 'Class variable content', '__init__': <function Person.__init__ at 0x0000028833B58820>, '__str__': <function Person.__str__ at 0x0000028833B588B0>, '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>}person1 __dict__: {'name': 'Kurt', 'last_name': 'Cobain'}

Creating Classes and Instance Pythonic and non-Pythonic

class Square:
def __init__(self, size=0):
if not isinstance(size, int):
raise TypeError("size must be an integer")

if size < 0:
raise ValueError("size must be >= 0")

self.__size = size * size def area(self):
return self.__sizet__width()
class Square:
def __init__(self, size=0):
self.size = size @property
def size(self):
return self.__size @size.setter
def size(self, value):
if not isinstance(value, int):
raise TypeError("size must be an integer")
if value < 0:
raise ValueError("size must be >= 0")
self.__size = value def area(self):
return self.__size * self.__size

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store