<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Timothy Pulliam</title>
    <description>The latest articles on DEV Community by Timothy Pulliam (@timothypulliam).</description>
    <link>https://dev.to/timothypulliam</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F91820%2F51246420-b75d-4f00-a717-d17ef47775d8.jpeg</url>
      <title>DEV Community: Timothy Pulliam</title>
      <link>https://dev.to/timothypulliam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/timothypulliam"/>
    <language>en</language>
    <item>
      <title>Azure Network Security Groups</title>
      <dc:creator>Timothy Pulliam</dc:creator>
      <pubDate>Sat, 15 Apr 2023 01:36:49 +0000</pubDate>
      <link>https://dev.to/timothypulliam/azure-network-security-groups-1jaf</link>
      <guid>https://dev.to/timothypulliam/azure-network-security-groups-1jaf</guid>
      <description>&lt;h2&gt;
  
  
  What Are NSGs?
&lt;/h2&gt;

&lt;p&gt;In the age of cloud computing, security has become a top priority for organizations that rely on the cloud to store, process, and transmit data. Azure, Microsoft's cloud computing platform, offers a variety of security features to ensure the confidentiality, integrity, and availability of your data. One such feature is Azure Network Security Groups (NSGs).&lt;br&gt;
Azure NSGs are a tool for implementing network security policies in your virtual networks. They allow you to define inbound and outbound traffic rules that govern the flow of traffic to and from your virtual machines (VMs). By applying NSGs to your VMs and subnets, you can create a secure network environment that minimizes the risk of unauthorized access, data breaches, and other security threats.&lt;br&gt;
Here are some key features and benefits of Azure NSGs:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Traffic filtering: NSGs enable you to define rules that allow or deny traffic based on criteria such as IP addresses, protocols, and ports. You can also create rules that prioritize traffic or limit the amount of traffic allowed.&lt;/li&gt;
&lt;li&gt;Network segmentation: By applying NSGs to subnets and VMs, you can segment your network into smaller, more manageable units. This makes it easier to monitor and control network traffic and reduces the risk of lateral movement by attackers.&lt;/li&gt;
&lt;li&gt;Integration with other Azure services: NSGs can be used in conjunction with other Azure security services, such as Azure Firewall and Azure Application Gateway, to create a comprehensive security solution.&lt;/li&gt;
&lt;li&gt;Logging and monitoring: NSGs provide logging and monitoring capabilities that enable you to track and analyze network traffic. This helps you detect and respond to security incidents and comply with regulatory requirements.&lt;/li&gt;
&lt;li&gt;Easy to use: NSGs can be configured using Azure Portal, Azure CLI, Azure PowerShell, or Azure REST API. This flexibility makes it easy to integrate NSGs into your existing workflows and automate security policies.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Azure CLI Example
&lt;/h2&gt;

&lt;p&gt;Here are the Azure CLI commands for creating an Azure Network Security Group:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a resource group (if one doesn't already exist):
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;az group create &lt;span class="nt"&gt;--name&lt;/span&gt; myResourceGroup &lt;span class="nt"&gt;--location&lt;/span&gt; eastus
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Create a network security group:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;az network nsg create &lt;span class="nt"&gt;--name&lt;/span&gt; myNetworkSecurityGroup &lt;span class="nt"&gt;--resource-group&lt;/span&gt; myResourceGroup &lt;span class="nt"&gt;--location&lt;/span&gt; eastus
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Create an inbound security rule:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;az network nsg rule create &lt;span class="nt"&gt;--name&lt;/span&gt; allow-http &lt;span class="nt"&gt;--nsg-name&lt;/span&gt; myNetworkSecurityGroup &lt;span class="nt"&gt;--priority&lt;/span&gt; 100 &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--source-address-prefix&lt;/span&gt; &lt;span class="s2"&gt;"*"&lt;/span&gt; &lt;span class="nt"&gt;--source-port-range&lt;/span&gt; &lt;span class="s2"&gt;"*"&lt;/span&gt; &lt;span class="nt"&gt;--destination-address-prefix&lt;/span&gt; &lt;span class="s2"&gt;"*"&lt;/span&gt; &lt;span class="nt"&gt;--destination-port-range&lt;/span&gt; 80 &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--access&lt;/span&gt; allow &lt;span class="nt"&gt;--protocol&lt;/span&gt; Tcp &lt;span class="nt"&gt;--description&lt;/span&gt; &lt;span class="s2"&gt;"Allow HTTP traffic"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Create another inbound security rule:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;az network nsg rule create &lt;span class="nt"&gt;--name&lt;/span&gt; allow-https &lt;span class="nt"&gt;--nsg-name&lt;/span&gt; myNetworkSecurityGroup &lt;span class="nt"&gt;--priority&lt;/span&gt; 110 &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--source-address-prefix&lt;/span&gt; &lt;span class="s2"&gt;"*"&lt;/span&gt; &lt;span class="nt"&gt;--source-port-range&lt;/span&gt; &lt;span class="s2"&gt;"*"&lt;/span&gt; &lt;span class="nt"&gt;--destination-address-prefix&lt;/span&gt; &lt;span class="s2"&gt;"*"&lt;/span&gt; &lt;span class="nt"&gt;--destination-port-range&lt;/span&gt; 443 &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--access&lt;/span&gt; allow &lt;span class="nt"&gt;--protocol&lt;/span&gt; Tcp &lt;span class="nt"&gt;--description&lt;/span&gt; &lt;span class="s2"&gt;"Allow HTTPS traffic"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Create an outbound security rule:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;az network nsg rule create &lt;span class="nt"&gt;--name&lt;/span&gt; allow-all &lt;span class="nt"&gt;--nsg-name&lt;/span&gt; myNetworkSecurityGroup &lt;span class="nt"&gt;--priority&lt;/span&gt; 100 &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--source-address-prefix&lt;/span&gt; &lt;span class="s2"&gt;"*"&lt;/span&gt; &lt;span class="nt"&gt;--source-port-range&lt;/span&gt; &lt;span class="s2"&gt;"*"&lt;/span&gt; &lt;span class="nt"&gt;--destination-address-prefix&lt;/span&gt; &lt;span class="s2"&gt;"*"&lt;/span&gt; &lt;span class="nt"&gt;--destination-port-range&lt;/span&gt; &lt;span class="s2"&gt;"*"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--access&lt;/span&gt; allow &lt;span class="nt"&gt;--protocol&lt;/span&gt; &lt;span class="s2"&gt;"*"&lt;/span&gt; &lt;span class="nt"&gt;--description&lt;/span&gt; &lt;span class="s2"&gt;"Allow all outbound traffic"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;These Azure CLI commands create an Azure Network Security Group resource called &lt;code&gt;myNetworkSecurityGroup&lt;/code&gt;, along with three security rules: &lt;code&gt;allow-http&lt;/code&gt;, &lt;code&gt;allow-https&lt;/code&gt;, and &lt;code&gt;allow-all&lt;/code&gt;. You can modify the parameters of these commands to suit your specific security requirements.&lt;/p&gt;
&lt;h2&gt;
  
  
  Terraform Example
&lt;/h2&gt;

&lt;p&gt;Here's an example Terraform code for creating an Azure Network Security Group:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Configure the Azure provider&lt;/span&gt;
&lt;span class="nx"&gt;provider&lt;/span&gt; &lt;span class="s2"&gt;"azurerm"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;features&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;# Create a new resource group&lt;/span&gt;
&lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"azurerm_resource_group"&lt;/span&gt; &lt;span class="s2"&gt;"example"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"example-rg"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"eastus"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;# Create a new network security group&lt;/span&gt;
&lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"azurerm_network_security_group"&lt;/span&gt; &lt;span class="s2"&gt;"example"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"example-nsg"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;azurerm_resource_group&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;example&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;resource_group_name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;azurerm_resource_group&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;example&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;
&lt;span class="c1"&gt;# Define inbound security rules&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;security_rule&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"allow-http"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;priority&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;direction&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Inbound"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;access&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Allow"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;protocol&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Tcp"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;source_port_range&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"*"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;destination_port_range&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"80"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;source_address_prefix&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"*"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;destination_address_prefix&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"*"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;security_rule&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"allow-https"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;priority&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;110&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;direction&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Inbound"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;access&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Allow"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;protocol&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Tcp"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;source_port_range&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"*"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;destination_port_range&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"443"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;source_address_prefix&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"*"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;destination_address_prefix&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"*"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;# Define outbound security rules&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;security_rule&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"allow-all"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;priority&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;direction&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Outbound"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;access&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Allow"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;protocol&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"*"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;source_port_range&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"*"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;destination_port_range&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"*"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;source_address_prefix&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"*"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;destination_address_prefix&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"*"&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This Terraform code creates a new Azure Network Security Group resource called &lt;code&gt;example-nsg&lt;/code&gt; and associates it with a new resource group called &lt;code&gt;example-rg&lt;/code&gt;. The &lt;code&gt;security_rule&lt;/code&gt; blocks define inbound and outbound traffic rules, allowing HTTP and HTTPS traffic inbound, and all traffic outbound. You can modify these rules to suit your specific security requirements.&lt;br&gt;
Here are the Terraform commands to run the code:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;terraform init&lt;/code&gt;: This command initializes the working directory and downloads any necessary providers or modules.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;terraform plan&lt;/code&gt;: This command creates an execution plan that shows what actions Terraform will take when you apply the configuration. It also shows any errors or warnings that need to be addressed before applying the configuration.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;terraform apply&lt;/code&gt;: This command applies the configuration and creates the Azure Network Security Group resource.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;terraform destroy&lt;/code&gt;: This command destroys the created resources, including the Azure Network Security Group resource.
To run these commands, navigate to the directory containing the Terraform code in your terminal or command prompt, and run the commands in order. You'll need to authenticate with your Azure account credentials during the &lt;code&gt;terraform init&lt;/code&gt; and &lt;code&gt;terraform apply&lt;/code&gt; commands.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In summary, Azure Network Security Groups are a powerful tool for implementing network security policies in your virtual networks. By defining traffic rules and applying them to your VMs and subnets, you can create a secure network environment that minimizes the risk of security breaches and data loss. With their integration with other Azure security services and easy-to-use configuration options, NSGs are a valuable addition to any organization's cloud security toolkit.&lt;/p&gt;

</description>
      <category>azure</category>
      <category>network</category>
      <category>security</category>
      <category>cloud</category>
    </item>
    <item>
      <title>Class Variables in Python</title>
      <dc:creator>Timothy Pulliam</dc:creator>
      <pubDate>Sat, 20 Mar 2021 23:05:59 +0000</pubDate>
      <link>https://dev.to/timothypulliam/class-variables-in-python-524o</link>
      <guid>https://dev.to/timothypulliam/class-variables-in-python-524o</guid>
      <description>&lt;h2&gt;
  
  
  Class / Static Variables in Python
&lt;/h2&gt;

&lt;p&gt;Consider the following python class&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

    &lt;span class="c1"&gt;# class / static variables
&lt;/span&gt;    &lt;span class="n"&gt;is_mammal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
    &lt;span class="n"&gt;genus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"felis"&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"daisy"&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# instance variables
&lt;/span&gt;        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# set / get methods
&lt;/span&gt;    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;set_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Class variables can be used without needing to instance the class&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_mammal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# True
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why Class Variables are Bad
&lt;/h2&gt;

&lt;p&gt;A good guiding rule is, do not use class variables unless absolutely necessary. The reason is simple; they break the most basic principle of Object&lt;br&gt;
Oriented Programming, which is that data should be &lt;strong&gt;encapsulated&lt;/strong&gt;. Other instances of a class should not have direct access to other instance's data members (variables). That would defeat the very purpose of creating separate instances with their own values to begin with.&lt;/p&gt;
&lt;h6&gt;
  
  
  Example
&lt;/h6&gt;

&lt;p&gt;You can change a class' static variable like so&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_mammal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"yes"&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_mammal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# yes
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, note that if you do, you will change the value for any existing instances, as well as future instances of the class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;daisy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;daisy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_mammal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# True
&lt;/span&gt;&lt;span class="n"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_mammal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"yes"&lt;/span&gt;
&lt;span class="n"&gt;sophie&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;daisy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_mammal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# yes
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sophie&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_mammal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# yes
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Only Use Class Variables For Constants
&lt;/h2&gt;

&lt;p&gt;Class variables exist at the &lt;strong&gt;class scope&lt;/strong&gt;. All instances of the class have access to these variables. In this regard they are similar to global variables and should only be used for storing global values that all instances of a class agree on (i.e. constants).&lt;/p&gt;

&lt;p&gt;In the cat class example, having a reference to the genus of the cat is probably fine as a class variable, since that is constant and should never change. However, you should designate these values as constants by typing them in upper case.&lt;/p&gt;

&lt;p&gt;The fact that class variables are similar to global variables should be enough of a deterrent to use them with extreme caution. Any variable that can be changed or read by many instances at any time creates a fundamentally unpredictable program.&lt;/p&gt;

</description>
      <category>python</category>
      <category>oop</category>
    </item>
  </channel>
</rss>
