Explanation how the computer runs this piece of these classes step by step

Hi,

Im new to python so thank you all for your support, really nice to find a nice forum with people that share the same interest for once.

Well, here i have a code here. Often when i follow someone, the code makes pretty much sence. But today i was thinking like, okey if i was sitting here alone now would i really understand why we wrote this code as we did or did i just understand the code itself but maybe not how the computer reads the code. ( if you understand what i mean)

So yeah, i found a piece here of this code that i found out that i actually dont think i can see in my head how the computer reads this code step by step and why i would write this piece of code like this.

Could someone explain step by step how the computers reads this piece:

In the sub class Admin(Users) we have the attribute or even an instance//object “self.privileges.Privileges(privileges)”
and the calling
“admin_1.privileges.show_privileges()”
what happens here? Why do we write it like this. Cause i would probably if iwas alone trying to write it:

“admin_1.Privileges.show_privileges()” “”“Calling the sub class directly, but i realise we have an attribute privileges as an list in our method that’s will be missing if i write it like this.”“”"

class User:

def __init__(self, first_name, last_name, location, age):
	self.first_name = first_name
	self.last_name = last_name
	self.location = location
	self.age = age
	self.login_attempts = 0
	
def describe_user(self):
	"""describes a user"""
	
	print(f"hello, {self.first_name} {self.last_name}!")
	print(f"The use is {self.age} years old and lives in {self.location}")
	
def greet_user(self):
	print(f"Hello, {self.first_name} {self.last_name}")
	
def incriment_login_attempts(self):
	"""increases login attemps"""
	
	self.login_attempts += 1
	
def reset_login_attempts(self):
	"""resets login attempts"""
	
	self.login_attempts = 0
	
"""child class of the head class 'User'"""

class Privileges:

"""To define or connect to Priviliges class"""
def __init__(self,privileges):
	self.privileges = privileges

def show_privileges(self):
	
	print("Admins privileges are")
	for p in self.privileges:
		print(f"- {p}")		

class Admin(User):

def __init__(self, first_name, last_name, location, age, privileges):
	self.first_name = first_name
	self.last_name = last_name
	self.location = location
	self.age = age
	self.login_attempts = 0
	
	self.privileges = Privileges(privileges) 

#test = User(‘firstname’,‘lastname’,‘Landvetter’,28)
#test.describe_user()
“”“instance of an object”“”
admin_1 = Admin(‘Firstname’,‘Lastname’,‘Europa’,28,[‘Ban users’, ‘Accept users’])
“”“calling”“”
admin_1.privileges.show_privileges()

I hope it makes sence. You all have probably been i my situation where you understand the code when you see it complete, but if you were alone you wouldnt be able to write it yourself because you dont fully understand it yet so you couldnt figure out why and how you should solve some things like what i described.

Is there any good website or video were they explain things like this, how the computer reads the code step by step so you better understand it, so it will be easier to write the code yourself? often it is just explanations of each parts of the codes but how it works step by step you often figure out yourself.

Well, here i have a code here. Often when i follow someone, the code
makes pretty much sence. But today i was thinking like, okey if i was
sitting here alone now would i really understand why we wrote this code
as we did or did i just understand the code itself but maybe not how
the computer reads the code. ( if you understand what i mean)

So yeah, i found a piece here of this code that i found out that i actually dont think i can see in my head how the computer reads this code step by step and why i would write this piece of code like this.

Before we start, when pasting code into a message (which we much
prefer over screenshots) please enclose it in triple backticks, eg:

 ```
 your code
 goes here
 ```

This preserves they indenting and other formatting. Your post looks
“mostly formatted” because it is mostly indented code, and in this forum
text indented by 4 spaces is presumed to be code. You’ll notice that in
your post the unindented code looks like prose, not code.

Anyway, the code.

This is an implementation of “users” using classes. The point of a class
is to encapsulate various bit of behaviour as methods: little functions
in the class which perform well defined tasks. These methods let the end
user perform these tasks by name without knowing the specifics of how
the class implements things
.

This is important:

  • it allows you to change the implementation internal code without
    breaking the code which is using it
  • it allows you to make different classes presenting the same interface
    (i.e. the same collection of methods), and have the same end user code
    use them the same way

This code defines an Admin user class which is like a User user
class with some additional features. In this case you’re looking at the
privileges. (I find it slightly odd that ordinary Users do not have
privileges - it would be more normal for ordinary Users to also have
privileges, just lesser privileges.)

There’s a Privileges class, which has a show_privileges method which
prints out the privileges. In principle this would let you write another
such class with a different implementation which also had a
show_privileges method. For example, this class looks like it expects
a list of privileges, whereas another implementation might consult a
database on the fly to get that information.

When you want to print the privileges it seems you’re expected to (a)
user the expression admin.privileges to obtain whatever privileges
objest is associated with the Admin user then (b) call
.show_privileges() on that object to print it out.

So to your question below:

Could someone explain step by step how the computers reads this piece:

In the sub class Admin(Users) we have the attribute or even an instance//object “self.privileges.Privileges(privileges)”
and the calling
“admin_1.privileges.show_privileges()”
what happens here? Why do we write it like this. Cause i would probably if iwas alone trying to write it:

“admin_1.Privileges.show_privileges()” “”“Calling the sub class directly, but i realise we have an attribute privileges as an list in our method that’s will be missing if i write it like this.”“”"

This expression:

 admin_1.privileges

obtains whatever privileges object is bound to the Admin user. In
this example code that will always be a Privileges instance. But in a
larger system this might be anything presenting the .show_privileges
method. The calling code does not need to care whether this is a
Privileges instance or some hypothetical PrivilegesFromADatabase
instance, because they would both have the same method.

Your suggested expression:

 admin_1.Privileges.show_privileges()

has a couple of problems. The first is that the admin_1 object has no
Privileges attribute, so it won’t work. But if we ignore that and
write it in a way which will work:

 Privileges.show_privileges(admin_1.privileges)

it has the problem that it assumes that admin_1.privileges is in
fact an instance of the Privileges class, rather than any object
with a show_privileges method.

This means that the end user code assumes stuff about the internal
implementation of admin_1. The purpose of methods is to avoid needing
that special knowledge, which leaves the larger code free to (a) change
how the Admin class provides .privileges or (b) to use a different
kind of privileges object as admin_1.privileges.

The original Object Oriented Methodology (OOM) design scheme has this
objective: to separate the internal implementations from the code which
uses it. It spoke in terms of “messages”: to print out privileges you’d
send a show_privileges “message” to the admin_1 object, and get back
a result. Thus having no access to the internals.

Python classes present such message/response pairs as methods, a type of
function bound to a class: calling a method corresponds “sending a
message” and the return value from the method function is the result you
get back.

Anyway, that is the larger design objective surrounding why the code is
written like this.

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like