<?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: .</title>
    <description>The latest articles on DEV Community by . (@justforaday__).</description>
    <link>https://dev.to/justforaday__</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%2F501542%2Fa460436c-973d-40f0-89d1-c2ba3429f3d4.jpg</url>
      <title>DEV Community: .</title>
      <link>https://dev.to/justforaday__</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/justforaday__"/>
    <language>en</language>
    <item>
      <title>Javascript vs Python: object</title>
      <dc:creator>.</dc:creator>
      <pubDate>Wed, 04 Nov 2020 09:14:14 +0000</pubDate>
      <link>https://dev.to/justforaday__/javascript-vs-python-object-1c03</link>
      <guid>https://dev.to/justforaday__/javascript-vs-python-object-1c03</guid>
      <description>&lt;p&gt;&lt;strong&gt;Python&lt;/strong&gt;&lt;br&gt;
Almost everything is an object (the &lt;code&gt;if&lt;/code&gt; statement isn't, for example, but every float, string or a function is an object). "Variables" are names, and "variable assignment" (like a = 6) is actually &lt;em&gt;binding of a name to an object&lt;/em&gt;. Each binding has a scope that defines its visibility.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A = []
B = A # both A and B are bound to the same object
B.append("hi") # object bound to B (and A) is mutated
print(A, B)
&amp;gt;&amp;gt;&amp;gt; ["hi"], ["hi"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So Python has mutable and immutable objects. For example, &lt;code&gt;list&lt;/code&gt; is mutable, &lt;code&gt;int&lt;/code&gt; is immutable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Javascript&lt;/strong&gt;&lt;br&gt;
In JS, we also say that almost everything is an object. What isn't an object are primitive values - null, undefined, string, number, boolean and symbol. So apart from the obvious differences with Python (like the fact that Python doesn't have &lt;code&gt;undefined&lt;/code&gt; and &lt;code&gt;null&lt;/code&gt; only &lt;code&gt;None&lt;/code&gt; and that is an object too), I want to talk a bit about what is meant by &lt;em&gt;object&lt;/em&gt; in either language.&lt;/p&gt;

&lt;p&gt;Object in JS is a "storage" of key:value pairs. Coming from python, when I first started using JS, I thought "aha, objects in JS are like dictionaries in python."&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myObj = {id: 5};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But better equivalent of a Javascript object in Python is not a dictionary, it's a class. So when we say an object in python, we mean an instance of some class. But, both languages are object-oriented. It's just that in JS, there is no distinction between a class and an instance of a class. &lt;/p&gt;

&lt;p&gt;Python: Class definition only says what methods and properties will every instance of it have, it's just a blueprint. Object/instance, is the actual house. &lt;/p&gt;

&lt;p&gt;JS: Since there are no classes (the blueprints) but only objects, each object is both the blueprint and the house.&lt;/p&gt;

&lt;p&gt;To demonstrate, we can add new methods to objects on the fly.&lt;/p&gt;

&lt;p&gt;Javascript:&lt;br&gt;
(note: the &lt;code&gt;class&lt;/code&gt; keyword is only syntactic sugar)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Box(id, label) {
    this.id = id;
    this.label = label;
}

const myBox = new Box(1, "stuff");

function open() {
  return `${this.label} is open`;
}

// add method to "class definition" (object)
Box.prototype.open = open; 

console.log(myBox.open()); // prints: stuff is open

function close() {
  return `${this.label} is closed`;
}

// add method to "class instance" (also object)
myBox.close = close;

console.log(myBox.close()); // prints: stuff is closed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would not be so easy in python:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Box(object):
    def __init__(self, id, label):
        self.id = id
        self.label = label 


def open(self):
    return f"{self.label} is open"

myBox = Box(1, 'stuff')

# add method to class definition
Box.open = open 

print(myBox.open()) # prints: stuff is open

def close(self):
    return f"{self.label} is closed"

# add method to class instance (object)? not so fast!
myBox.close = close 

print(myBox.close()) # throws a TypeError: 
# close() missing a required argument
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So in python, it is not so easy to add methods to an object (which is an instance of a class) on the fly, &lt;a href="https://docs.python.org/3/library/types.html#types.MethodType"&gt;but it is possible: &lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import types

myBox.close = types.MethodType(close, myBox)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The difference between python and Javascript can be thought of as a difference in approach to solve the OOP concept. Which is what this example illustrates - it's just that you can't do it in python the same way you would in JS. &lt;/p&gt;

&lt;p&gt;Okay. One last thing - the concept of pass by value vs. pass by reference. In short - pass by value means that if we have a variable &lt;code&gt;a&lt;/code&gt; and then pass it to some function that modifies it, and then look at &lt;code&gt;a&lt;/code&gt; again, the changes made inside function will not be reflected. With pass by reference, they will. &lt;/p&gt;

&lt;p&gt;Python and JS on the surface, behave the same way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;s = "hello"
def add(arg):
    arg = arg + "bye"
    return arg

add(s) # the returned value doesn't have a name
# bound to it here, but it would be "hellobye"
print(s) # "hello"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and it would behave exactly the same in JS.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let s = "hello";

function add(arg) {
  arg = arg + "bye";
  return arg;
}

add(s); // no assignment to return value
console.log(s); // hello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However the concept behind these two is different. In Python, string is an immutable object. In Javascript, string is a &lt;em&gt;primitive&lt;/em&gt; data type, not an object. And primitive data types are immutable. &lt;br&gt;
So passing a primitive data type in JS(or immutable object in Python) to a function, will give you the "pass by value" behavior. And passing an object in JS (or mutable object (e.g. list) in Python) will result in "pass by reference" behavior. &lt;/p&gt;

&lt;p&gt;And emphasis on &lt;em&gt;behavior&lt;/em&gt; because things are implemented differently in different languages. For example in C/C++, "reference" has a very specific meaning, but in Python, it doesn't exist. More on this &lt;a href="https://www.tutorialspoint.com/Can-we-assign-a-reference-to-a-variable-in-Python"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Sources: &lt;br&gt;
&lt;a href="https://www.jeffknupp.com/blog/2012/11/13/is-python-callbyvalue-or-callbyreference-neither/"&gt;1 Is Python call by value or call by reference? Neither&lt;/a&gt;&lt;br&gt;
&lt;a href="https://medium.com/background-thread/why-javascript-is-an-oop-language-even-though-it-doesnt-have-classes-92a4e202176f"&gt;2 Why JS is OOP even though it doesn't have classes&lt;/a&gt;&lt;br&gt;
&lt;a href="https://stackoverflow.com/questions/972/adding-a-method-to-an-existing-object-instance"&gt;3 stack overflow question&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>python</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Division by zero in pure python vs numpy</title>
      <dc:creator>.</dc:creator>
      <pubDate>Tue, 03 Nov 2020 14:12:45 +0000</pubDate>
      <link>https://dev.to/justforaday__/division-by-zero-in-pure-python-vs-numpy-2d8p</link>
      <guid>https://dev.to/justforaday__/division-by-zero-in-pure-python-vs-numpy-2d8p</guid>
      <description>&lt;p&gt;Today I learned that if you're trying to divide by zero and that zero is a Numpy type, the compiler will not raise the &lt;code&gt;ZeroDivisionError&lt;/code&gt; but only &lt;code&gt;RuntimeWarning&lt;/code&gt; and it will assign &lt;code&gt;numpy.inf&lt;/code&gt; to the value. &lt;code&gt;numpy.inf&lt;/code&gt; is IEEE 754 floating point representation of positive infinity, &lt;a href="https://numpy.org/doc/stable/reference/constants.html"&gt;according to the docs&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import numpy as np

try:
    a = 1 / 0
except ZeroDivisionError:
    print("You can't divide by zero!") 

try:
    b = np.intc(1) / np.intc(0)
    print('Hello numpy, b is: ', b)
except ZeroDivisionError:
    print("Can you divide by zero?")

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;You can't divive by zero!
&amp;gt;&amp;gt;RuntimeWarning: divide by zero encountered in int_scalars
  num = np.intc(1) / np.intc(0)
Hello numpy, b is:  inf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>beginners</category>
      <category>numpy</category>
    </item>
    <item>
      <title>console.time(), parsing api response as json and a little test python server</title>
      <dc:creator>.</dc:creator>
      <pubDate>Thu, 29 Oct 2020 12:44:23 +0000</pubDate>
      <link>https://dev.to/justforaday__/console-time-parsing-api-response-as-json-and-a-little-test-python-server-bl9</link>
      <guid>https://dev.to/justforaday__/console-time-parsing-api-response-as-json-and-a-little-test-python-server-bl9</guid>
      <description>&lt;p&gt;I wanted to set up a fake API that gives me test data and find out how long does my javascript function (that is wrapped deep inside some react components) execute. &lt;/p&gt;

&lt;p&gt;Python - fake API&lt;/p&gt;

&lt;p&gt;server.py:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask import Flask, jsonify
from flask_cors import CORS

app = Flask(__name__)
cors = CORS(app)
@app.route('/get-data', methods=['GET'])
def get_data():
    data = getData() # generate fake data, much bigger than real
    return jsonify(data)


if __name__ == "__main__":
    app.run()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Javascript - index.html&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;html&amp;gt;
&amp;lt;body&amp;gt;
&amp;lt;script&amp;gt;
function doExpensiveThings() {
   ...
}

fetch('http://localhost:5000/get-data', {mode: 'cors'}).then(response =&amp;gt; {
    response.json().then(jsonResponse =&amp;gt; {
        console.time("doExpensiveThings");
        doExpensiveThings(jsonResponse);
        console.timeEnd("doExpensiveThings");
    }).catch(err =&amp;gt; {
        console.warn(err);
})
}).catch(err =&amp;gt; {
    console.warn(err);
})

&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then in the console, one would see: &lt;code&gt;doExpensiveThings: 1.3291015625 ms&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;fetch&lt;/code&gt; returns a promise, if everything went alright, the promise is resolved and the value is an instance of the Response class. I wanted to parse the response as json, so I called &lt;code&gt;.json()&lt;/code&gt; method on the instance, this again returns a promise. &lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
