0
class Student:

    def __init__(self, name, no, laptop):
        self.name = name
        self.no = no
        self.laptop = laptop

    class Laptop:

        def __init__(self, brand, cpu, ram):
            self.brand = brand
            self.cpu = cpu
            self.ram = ram

        def AddRam(self):
            self.ram += 16



s1 = Student("John Doe", 41,Student.Laptop("Asus","I7 10950H",16))
print(s1.name)
print(s1.laptop.brand, s1.laptop.cpu, s1.laptop.ram)
s1.laptop.AddRam()
print(s1.laptop.brand, s1.laptop.cpu, s1.laptop.ram)

I'm trying to understand the logic of inner classes, if the usage is not true, how should it be?

I mean, is it right way to use the laptop parameter in outer class or it should be in the inner class ?

Yigit
  • 31
  • 4
  • 2
    Is there a particular reason you need `Laptop` to be an inner class of `Student`, instead of its own outer class? Usually, inner classes represent sub-data structures that are dependent on the outer class to be useful (e.g. a `Node` in a `LinkedList`), not independent data structures that can stand on their own. – Green Cloak Guy May 27 '21 at 15:37
  • 1
    Sure, I don't see anything wrong with it. If `Laptop` is only meant to be used with `Student`, then it's fine. If `Laptop` is meant to be its own entity that might be used without `Student` one day, then maybe not. But nesting classes in Python is really just a namespacing and organization technique; it has no real effect on semantics other than how you refer to things. You may also find [this question](https://stackoverflow.com/questions/719705/what-is-the-purpose-of-pythons-inner-classes) helpful (avoid the first answer, though; it's a Java answer on a Python question for some reason) – Silvio Mayolo May 27 '21 at 15:39
  • 1
    Also, since you ask about correctness, it's worth noting that [Python's official style guide](https://www.python.org/dev/peps/pep-0008/) (and most Python programmers, for that matter) recommends `snake_case` for function names, not `PascalCase` like you have for `AddRam` (the latter is more of a C# convention). – Silvio Mayolo May 27 '21 at 15:40
  • @GreenCloakGuy I want the laptops to be related with each student, is there any other way to do this or is it unnecessary ? – Yigit May 27 '21 at 15:50
  • @SilvioMayolo Thank you.. Yeah i usually use function names as you suggested, I couldn't understand why I wrote it like that here x) – Yigit May 27 '21 at 15:52
  • 1
    I would personally just make `Laptop` an ordinary top-level class. You can define it in the same file as `Student` (Python has no restrictions on how many classes appear in a file). A nested class is really meant for cases where the inner class has no meaning without the outer class. GreenCloakGuy's node example is good, because a `Node` is *never* useful outside of the data structure it's in, whereas a `Laptop` can be perfectly useful without a `Student` (perhaps it belongs to an instructor, or perhaps it hasn't been sold yet and is sitting on a shelf somewhere) – Silvio Mayolo May 27 '21 at 15:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/232965/discussion-between-yigit-and-silvio-mayolo). – Yigit May 27 '21 at 18:18

0 Answers0