Friday, June 5, 2020

Lambda, Class and Objects

 Lambda

Lambda functions are anonymous functions and it has to all fit on one line. You can have some mixing of types, for example, int and float, in the lambda's expression.

price = lambda tag : tag + 10
print(price(0.99))

# result is:

10.99

The variable as assigned is the Lambda. You must send in values for all the parameters even if not using it in the expression of the Lambda, though this is poor coding anyhow.


price = lambda dollar_sign, tag : tag + 10
print(price("$", 0.99))
# no error


Lambda looks like a confusing roundabout way to just do traditional functions. I don't see its real purpose.


 Classes and Objects


The class keyword is used to make an object and the object will have methods and properties. After the class is created an object can be defined by calling the class with a pair of empty ().

class groceries:
         meats = "beef"
         breads = "wheat"

dinner = groceries()

print(dinner.meats)

# result is:

beef


A more powerful and practical way of creating classes is to pass in arguments to object's properties.

Don't forget the colon after the class and def lines!!
Don't forget to indent the properties under the init call!!

class groceries:
    def __init__(this_object, protein, carbs):
        this_object.meats = protein
        this_object.breads =  carbs

dinner = groceries("beef", "wheat")

print(dinner.meats)

# result is:

beef


The __init__ it takes the place of a primary list in a function, as would normally  be represented by () with parameters inside on the function def line.

def function(a, b, c) is sort of like def  __init__(self, a,b, c)


Every class has a built-in default self-referencing variable which is the first parameter in the __init__ call. If you try to access properties of the class without using the self-referencing variable you'll get a TypeError that says "0 positional arguments but one was given" 


Using an object's function out of scope will return a null blank value. Meaning, just calling the function name "naked" without its object "owner" returns a blank.


Objects can access other objects' properties by default, but like I just said you must use a self-referencing object variable name for any access to a property, even within a method of the class itself!!

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def myfunc(self): # this function named myfunc belongs to Class Person
    print("Hello my name is " + p2.name) # using anotherPerson object property - allowed

class anotherPerson:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def myfunc(self): # this function named myfunc belongs to Class anotherPerson
                               # even though ti has the same name as in the Person class
    print("Hello my name is " + p1.name)

p1 = Person("John", 36)
p2 = anotherPerson("Mary", 22)
p1.myfunc()
p2.myfunc()

# will print Mary in John's place and vice versa


Within a class, the object properties (via init vs via def an internal function) are cross recognized between init and def.



class groceries:
  def __init__(foodobject, fruit, nuts):
    foodobject.sweet = fruit
    foodobject.protein = nuts

# anything can use foodobject's name property
  def anyfunc(anything):
    print("Hello my dessert is " + anything.sweet)

meal = groceries("cherry", "pecan")

meal.anyfunc()

# result is 
My fruit is cherry

snack = groceries("lemon""walnuts")
snack.anyfunc()


# result is 
My fruit is lemon


However, within a class, the use of  that different name for the self-referencing object variable across properties (via init vs via def an internal function) is NOT cross recognized.

class groceries:
  def __init__(foodobject, fruit, nuts):
    foodobject.sweet = fruit
    foodobject.protein = nuts

# the anyfunc using the init's self-referencing object variable foodobject is incorrect.

  def anyfunc(anything):
    print("Hello my dessert is " + 
foodobject.sweet)

snack = groceries("lemon""walnuts")
snack.anyfunc()


# result is an error because foodobject only belongs to __init__
# another way to think about this is that foodobject masquerades as # anything while inside the anyfunc function. The first parameter
# to functions inside of a class is always the self-referencing
# variable.

NameError: name 'foodobject' is not defined.



Use the del keyword to properly remove a property from an object; however, del does not remove it from the class definition.

Use del keyword to remove the entire object and use pass for an empty class.










No comments:

Post a Comment