Posts

Showing posts from October, 2024

Python POOP: "name mangling"--how to make class data private or public (odd in Python!)

https://www.youtube.com/watch?v=6cvFzLB6hbA they way Python OOP ("Poop") deals with private and public data in classes is strange indeed. by default all class attributes and methods are public (so, no encapsulation, of any kind, at all) There is no reserved words "public" private protected that the interpreter recognizes. To get around this python employs "name mangling": __x = 7 two underscores means the variable or method is private to the class. If you go outside the class and try to print __x you get an error. However, you can still see the value for this name-mangled variable:  _classname.__x  (one underscore classname, dot, two underscores, variable name) So it's not really private, it is psuedo-private.   Whatever.

Python POOP: static, class methods

 https://www.youtube.com/watch?app=desktop&v=rq8cL2XMM5M You want to create a method that applies to every instantiated object in class "Employee" @classmethod  #keyword def raise_everyone(cls, amount):     cls.raise_amount = amount To use: Employee.raise_amount = 1.05

MAKING SOME POOP==Python classes constructor and instantiating; class variables

https://www.youtube.com/watch?v=ZDa-Z5JzLYM    << intro video on POOP https://www.youtube.com/watch?v=BJ-VvGyQxho&t=1s << class variables. SYNTAX IS A BIT FUCKED UP FOR PYTHON POOP. Easiest way to get going: class Employee:     pass Constructor uses odd syntax "self". I hear self can be anything you want, not sure why they didn't use something like (UserInstantiated) Self can be anything but everyone uses self --we require attributes for firstname, lastname, and pay. class Employee:      def __init__(self, firstname, lastname, pay):          self.first           self.last            self.pay           self.email = first + '.' + last + '@piscesinc.com'      Once constructor is set, you instantiate: emp1 = Employee("charlie", "lamm",50000) emp2 = Employee("Bob", "Kaake",70000) To add a method--creating ful...