Little help with exercise 41

i have been debugging code of exercise 41 from last 4 days and now i got all in and out of this exercise but still got one silly question in my mind

on line 36 how other_names is a list
i have checked type of other_names by printing type(other_names) that gives me <class ‘list’>
it is pretty clear one line above on line 35 that list comprehension is used but on line 36 there is no sign of even [] brackets so i’m bit curious that what made other_names a list

You mean this on line 37?

If yes, just check what random.sample() function does. Google it and you will find:
sample() is an inbuilt function of random module in Python that returns a particular length list of items chosen from the sequence i.e. list, tuple, string or set.

yes i googled it and found that python doc says
random. sample ( population , k )
" Return a k length list of unique elements chosen from the population sequence"
in the case of EX41 population is list WORDS and k is the count of “***” in particular snippet
so this sample function returning a list or string

what is the meaning of last phrase drill from EX41

foo.K = Q “From foo, get the K attribute, and set it to Q.”

what i understood is

foo is an object of some class (may be A) and getting the attribute K (which can be variable) and setting it to Q

correct me if i’m wrong

but my question is what is Q ?

Yes, basically foo is an instance of a class A().
then say we have this:

class A(object):
    def __init__(self):
        self.K = "bjgrft"
foo = A
Q = "whatever"
foo.K  = Q
foo.K == "whatever"
True
In this example Q can be any other thing you wanted. You can play in the Python interpreter in your terminal and see for yourself. Write the code above. Make Q equal to any string you wanted, any integer , etc.

as you said to run above code, i did that in two way
one as @io_io you said foo = A
and second as @zedshaw shown in EX40 like calling a class like function foo = A()
i have changed your code a bit

class A(object):
    def __init__(self):
        self.K = "bjgrft"
        print("self = ",self)
foo = A
print("foo = ",foo)
print("A = ",A)

Q = "whatever"
print("Q = ",Q)

#print("foo.K = ",foo.K)

foo.K  = Q
print("foo = ",foo)
print("foo.K = ",foo.K)
print("Q = ",Q)

print("foo.K == 'whatever'",foo.K == "whatever")

output of this is
A
if i don’t comment line 12 which is [print("foo.K = ",foo.K)] then i got error which is

AttributeError: type object 'A' has no attribute 'K'

and by second way calling class like function

class A(object):
    def __init__(self):
        self.K = "bjgrft"
        print("self = ",self)
foo = A()
print("foo = ",foo)
print("A = ",A)

Q = "whatever"
print("Q = ",Q)

print("foo.K = ",foo.K)

foo.K  = Q
print("foo = ",foo)
print("foo.K = ",foo.K)
print("Q = ",Q)

print("foo.K == 'whatever'",foo.K == "whatever")

i got output like
A()

can you explain difference between two ways
in second way i have not put comment on line 12 as before
and the difference i’m seeing in both way is

  • self is printed in second way, not in first way
  • getting an memory location of object foo in second way
  • in first way i can’t fetch value of K which is on line 12, in second way i can fetch the K

and also can you tell me difference between instance and object
because zed says in section objects are like class of ex40 that

When you instantiate a class what you get is called an object.

and in word drill of ex41 zed says
object = Two meanings: the most basic type of thing, and any instance of some thing.
instance = What you get when you tell Python to create a class.

Class Thing(object):

An instance of this class, is an object in memory. The terminology is where I struggled too.

Think:
Apple is a fruit, is food, is object.

So my “Thing ()” is an instance-“active copy of my class” is also an object.
The class , specifically the written code is the design for my thing. Creating an instance of it tells python to build it, it has the code qualities that make it an object.
Hope that helps!

3 Likes

so basically your are saying that class is not in real or they are shady thing and when we instantiate a class then python gives a instance(copy or example) of that class and object is making that instance into a reality or making physical (means placing into a memory) so indirectly class comes to reality
as you said apple is a fruit, is food, is object
so fruit is class and they are not in real fruit is just a name
now we have an apple which is instance or example of fruit and orange also
now apple and orange are in real they are physical so they are objects
this is what you are trying to say?

1 Like

The words are descriptors.
Lables for the piece of code that makes up a class.

Granny Smith apples, red deliscuos apple, sour apple. All apples. They’re descriptions.

The piece of code that is the class…
Until you create that “instance”. The code is just an object waiting to be used. Class and instance and object are all the same piece of code. How it’s being used decides it’s label.

I write the code, I save the file. The class piece of code, when called by making the instance:
car=GMCModeltruck()
car is now the actively stored class that the compiler can call upon and use. It can use it’s methods, and access initiated attributes.

so car is instance or object?

The car, in the example is an instance and an object.
Object just refers to the kind of thing it is. It’s not a function, it’s not a list…
It’s an object.
I’m gonna make you a bit of code you can run, but I need coffee first.

All these labels are just ways to identify that same piece of code, it’s purpose, and give python compiler directions what to do with it.

i like to thank you for putting your time and effort to clear my doubt
you mentioned labels which are name for class and class is a piece of code, right?
and “Granny Smith apples, red deliscuos apple, sour apple. All apples. They’re descriptions.” which are all individual instances or examples of class, right?
and they all are also an object of class, right?

1 Like

It’s all just labels for the compiler to put stuff on neat little shelves so it can get them when it needs to.

Here’s that code. I don’t care if you copy and paste, I learn by seeing something, so this always helps me.

#  class instance object

class SimpleExample(object):
    def __init__(self):
        self.alist = []
        self.dictionary = {}
        self.integer = 5
        self.string = "spam lovely spam"

    def simpleExamples_method(self):
        print("I am a method within the SimpleExample class")

def test_class_types():
    print("~~~~~~~    START     ~~~~~~~")
    print("objects can be full of useful things.")
    try:
        SimpleExample.simpleExamples_method()
    except Exception as e:
         print("we can not use a class that is not yet given a namespace in compiler memory.")
         print("error = \n", e)
         print("\n")
         print("that self is a whole different terminology having to do with the class being 'started' -- initiated.")
    # lets tell the compiler to make an instance of our class
    spam_on_a_can = SimpleExample()
    # Now lets try to use the 'instance's method
    try:
        print("\n")
        print("-------------")
        spam_on_a_can.simpleExamples_method()
    except Exception as e:
         print(e)
    # lets see what python **LABELS** each of these as:
    print("What type of thing is the SimpleExample?")
    print("\t", type(SimpleExample))
    print("\n")
    print("What type of thing is the spam on a can?")
    print("\t", type(spam_on_a_can))
    print("what does python see the spam on a can as in memory?")
    print("\t", spam_on_a_can)
    print("what about the SimpleExample, what does python label that as?")
    print("\t", SimpleExample)
    print("~~~~~   END    ~~~~~")


test_class_types()
2 Likes

and another piece of code for you to try. Feel free to mangle this and experiment.

# more class object instance

class SimpleExample():  #<== lets take object out
    def __init__(self):
        self.alist = []
        self.dictionary = {}
        self.integer = 5
        self.string = "spam lovely spam"

    def simpleExamples_method(self):
        print("I am a method within the SimpleExample class")

class SimpleExample2(object):  #<== lets make another with object
    def __init__(self):
        self.alist = []
        self.dictionary = {}
        self.integer = 5
        self.string = "spam lovely spam"

    def simpleExamples2_method(self):
        print("I am a method within the SimpleExample2 class")

print("What is SimpleExample Python?")
print("\t", type(SimpleExample))
print("What is SimpleExample2 Python?")
print("\t", type(SimpleExample2))

# lets make an instance of both
object_1 = SimpleExample()
object_2 = SimpleExample2()
print("~~~  After an instance of the class's are made ~~~~")
print("What is object_1  Python?")
print("\t", type(object_1))
print("what is the objects address?")
print("\t", object_1)
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")

print("What is object_2  Python?")
print("\t", type(object_2))
print("what is the objects address?")
print("\t", object_2)

print("\n~~~~~~~ What happened to the original class's code's label?")
print("class object SimpleExample:  ")
print("\t", type(SimpleExample))
print("class object SimpleExample2: ")
print("\t", type(SimpleExample2))

print("\n   How does python know what is an instance?  It's labeled.")
print("Is object_1 an instance of SimpleExample?")
print(isinstance(object_1, SimpleExample))

last line of second example clear me a bit but i want your conformation
what i learned from your code is object is just a label or name and memory location of instance and instance is copy of class (piece of code)
python know’s about every instance with it’s label and memory location (which is object)
and init method is used to initialized your instance into memory through self so self is an instance

1 Like

Correct. Also a class does not have to have an __ init __ to be used to store things. Try it out.
Initiating stuff is just saying I want my class to store these specific items for use later, and I want them to be related to, belong to this class I have defined.
That’s messy too.

self I also not any special word in python. You can do:
Class SpamALot(object):
Def init(spam):
spam.astring = “this string belongs to the SpamALot class” .

more code for you to try out:

# class's and initiating

class SpamALot(object):
    def __init__(spam):
        spam.astring = "This string belongs to class SpamALot"

tryit = SpamALot()
print(tryit.astring)

class NoSpam(object):
    alist_of_fruits = ['apples', 'oranges', 'pears']

tryit2 = NoSpam()

print(tryit2.alist_of_fruits)

okay so instance will be initialized to first parameter of init method and that parameter can be anything, can be your name or mine, right?
and init method is just used to make container named first parameter of init method and that container contains all the code of class which is an instance of class and this instance is stored in memory by some label or name which is object and python returns this instance to an object or name, right?

An instance doesn’t have to have any parameters.
The base class doesn’t either.
Initiating parameters is optional.
A class is just a Tupperware container who’s label can stick to everything inside.
The instance is just the copy of that container the compiler can take off the shelf, open up and use what’s inside.
I’m not sure if using so many words to describe it is necessary. Just play with the code, make it do tricks, you’ll see.