Classes in Julia

How do we create classes in Julia that involves the init part in Python?

Julia does not have classes in the object-oriented sense as it is not an object-oriented language. The main paradigm of Julia which replaces object-oriented programming is multiple dispatch, see here.

That said, the closest analogy to a Python class is a mutable struct (though you should default to using struct when possible). The biggest difference between these and a Python class is that in almost al use cases these do not “own” methods, only data. These are initialized with constructors which are analogous with the Python init method.

4 Likes

Out of curiosity. What exactly does it mean to say that Julia is not object-oriented?

big question.

A language that is designed to be “object-oriented” is, at its philosophical and pragmatic core, a playground for sculpting with your own or other persons’ clay. Objects are empty until they become designed with purposeful capabilities; and then they become both coin and cloth of the computational relm. Objects are computational aor user interactive wellsprings; the water is good.

Julia is its own motive experience, one that continues to unfold and advance. We write software more as expressive design, expertly informed and easily realized. The entities that compute, cooperate – how? In Julia, dispatch over types is really multidispatch over kinds of types and guided placement of realizable intent.

2 Likes

I take it to mean that methods are not owned by classes.

I don’t claim any formal computer science knowledge, and I can imagine the distinction being rather hazy snice, for example, structs certainly can own methods in any language with multiple dispatch and first-class functions.

As a practical matter, one of the biggest differences between writing Julia code and writing Python code is simply that you don’t define functions within structs, nor are functions necessarily associated with specific structs when you call them. If you come to Julia from C++ like I did, this immediately seems like a very big difference (and, in my opinion, one is infinitely better off in the Julia case).

6 Likes

Mostly that it doesn’t have classes. Despite classes having no place in Alan Kay’s original conception of object oriented programming, but these days when people say “object oriented” they mean “like C++, Java and Python” :man_shrugging:

3 Likes

Can someone maybe give an example of how these structs and types work assuming that you have to define the __init__part in a class in python

Thank you

It’s unclear (to me, at least) quite what you’re asking here. Do you want to know how to construct instances of types in Julia? Using __init__ in Python as a reference point may not be helpful since that’s not how things work in Julia. Have you read the section on composite types (aka “structs”) in the Julia manual? If so, was there something in there that was unclear?

2 Likes

Take for example this python code, how can you translate to Julia

class Employee:
empCount = 0

def init(self, name, salary):
self.name = name
self.salary = salary
Employee.empCount += 1

def displayCount(self):
print(“Total Employee %d” % Employee.empCount)

def displayEmployee(self):
print("Name : ", self.name, ", Salary: ", self.salary)

emp1 = Employee(“Zara”, 2000)
emp2 = Employee(“Manni”, 5000)

emp1.displayEmployee()
emp2.displayEmployee()

print(“Total Employee %d” % Employee.empCount)

I always liked this blog and I think it can help people understand the difference with an actual example.

Perhaps something like this

const GLOBALS = Dict(:empCount=>0)
                  
                   
struct Employee    
    name::String   
    salary::Float64
   
    function Employee(name::AbstractString, sal::Real)
        GLOBALS[:empCount] += 1
        new(name, sal)
    end
end


display_employee_count() = println("Total Employees: $(GLOBALS[:empCount])")
# could also  do
# display_count(::Type{Employee})

function Base.show(io::IO, emp::Employee)
    println(io, "Employee (Name: $(emp.name), Salary: $(emp.salary))")
end
   

emp1 = Employee("Zara", 2000)
emp2 = Employee("Manni", 5000)

show(emp1)
show(emp2)

display_employee_count()

Note that there are lots of options for dealing with the global variable Employee.empCount (as it’s called in your Python code). Ideally, I’d recommend eliminating this global. You probably can: would you perhaps like to put the Employees in a Vector? That’s just one example of a data structure that might contain or reference the Employees which maintains a count for you. You also don’t necessarily have to use Base.show, you could just define a new function for display_employee (with or without the IO argument), but Base.show will be called any time Julia wants to display an Employee (for example, just do emp1 in the REPL after the above).

Again, lots of ways of doing much of this, but the above examples is very close to your Python code.

5 Likes

So basically you can write a function inside structs? In a case where I would like to eliminate the global, how will that be possible?
Am still new to julia so I would like to see a simple code without the Base.show.
Thanks for your enlightenment.

I recommend you spend some time reading the documentation, in particular the section on constructors. Constructors are “special” functions in that they are the only functions that can be defined within a struct. Again, you’ll need to read the documentation to understand why.

It’s hard to say how exactly you’d elminate the global without more context. Like I said, I’d normally expect your Employees to be stored in some other data structure (perhaps just a Vector) that counts them for you. This would not be any different than in Python by the way, we just tend to be a bit more performance conscious in Julia. Nothing will “go wrong” if you do use a global, it just likely isn’t the best choice.

5 Likes