this post was submitted on 12 Sep 2024
289 points (96.2% liked)

Programmer Humor

19471 readers
1357 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS
 

var Turtle1 var Turtle2 var Is_Turtle

you are viewing a single comment's thread
view the rest of the comments
[–] wesker@lemmy.sdf.org 9 points 1 month ago* (last edited 1 month ago) (21 children)

Have you considered using inheritance?

[–] OttoVonNoob@lemmy.ca 4 points 1 month ago (14 children)

I'm an amateur I'm not sure what inheritance is:X? Is it like instantiateing?

[–] ma1w4re@lemm.ee 3 points 1 month ago* (last edited 1 month ago) (1 children)

Inheritance established "is a" relationship between classes.

class Turtle;  
class TigerTurtle is a Turtle (but better);  
class BossTurtle is a Turtle (but better);  

Underlying classes hold an inner object to the super class, everything from Turtle will be in TigerTurtle and BossTurtle.

In some languages that is configurable with public, private, protected keywords.

Relatedly, there's also composition, which establishes a "has a" relationship:

class TurtleTail;
class Turtle:
  var tail: TurtleTail; (has a tail);

Since Turtle is NOT a tail, but a whole animal, turtle should not inherit TurtleTail. But it HAS a tail, thus we add turtle tail as a property.

[–] _stranger_@lemmy.world 3 points 1 month ago* (last edited 1 month ago) (1 children)

I'm only commenting because the actual python is practically pseudo code:


# A turtle class
class Turtle:
    shell=True

# A boss class
class Boss:
    authority=True

#A class that inherits from another
class TigerTurtle(Turtle):
    fuzzy=True

# Multiple inheritance, or "The Devil's Playground"
class TigerBossTurtle(TigerTurtle, Boss):
    #  shell, authority, and fuzzy are all true
    ...
[–] ma1w4re@lemm.ee 2 points 1 month ago

It is simpler than my faulty memory remembers, time to learn python again 😁

load more comments (12 replies)
load more comments (18 replies)