Subjects
PRICING
COURSES
SIGN IN
Start Free Trial
Mayank S.
Deep Learning Engineer
Tutor Satisfaction Guarantee
Python Programming
TutorMe
Question:

Is it really correct to declare all instance attributes in __init__

Mayank S.
Answer:

8 down vote accepted Yes, you should assign all attributes of your object in the __init__ method. The most important reason to do this is tool support. Many Python IDEs can see the assignments in the __init__ and use those attributes for type-based autocompletion. Sphinx-doc can list those attributes in your reference documentation. There's also a more fundamental reason: Simple is good, so don't create complicated objects that change their shape during execution. Given a StringHandler instance I may or may not be able to read the handler.strings attribute. If I try that before handler.parse() has been executed, I will get an AttributeError. Afterwards I would get the value. To avoid exceptions, consumers of your class would have to getattr(handler, 'strings', None) with a default value which is quite tedious. In general, constructors should create fully-initialized objects that are immediately usable. In some cases it is necessary to create mutable objects that have an empty, default state, and later transition to a state where they contain a value. However, this should be the exception and is rarely a good design. See also Are init() methods a code smell? and Start Method vs. Setting up everything in constructor which discuss this issue for Java. In your case the parse() method is such a two-phase initialization method that must be called before the object can really be used. This raises the questions: What is the point of these objects, if they're just a container for whatever values the parse() function produces? As you are using them, you might as well use a dict instead of a class. Is an empty Handler meaningful in any way? Why is the initialization in parse() separate from the other initialization? Or are you using those attributes only for communication between the parse_x functions, not for external consumers? Possible alternative designs: The handlers specify how a field should be parsed, but do not actually contain the value. The parse method returns the value. Because the handlers do not contain any data, it may not be necessary to use classes for this – simple functions could are sufficient. The handlers represent a parsed value. There is no “empty” state. The parsing either happens in the constructor, or in separate functions. Because these objects are just dumb structs, you could define them via namedtuple() when parsing is separate.

Numpy
TutorMe
Question:

Is it worth my learning NumPy?

Mayank S.
Answer:

NumPy's arrays are more compact than Python lists -- a list of lists as you describe, in Python, would take at least 20 MB or so, while a NumPy 3D array with single-precision floats in the cells would fit in 4 MB. Access in reading and writing items is also faster with NumPy. Maybe you don't care that much for just a million cells, but you definitely would for a billion cells -- neither approach would fit in a 32-bit architecture, but with 64-bit builds NumPy would get away with 4 GB or so, Python alone would need at least about 12 GB (lots of pointers which double in size) -- a much costlier piece of hardware! The difference is mostly due to "indirectness" -- a Python list is an array of pointers to Python objects, at least 4 bytes per pointer plus 16 bytes for even the smallest Python object (4 for type pointer, 4 for reference count, 4 for value -- and the memory allocators rounds up to 16). A NumPy array is an array of uniform values -- single-precision numbers takes 4 bytes each, double-precision ones, 8 bytes. Less flexible, but you pay substantially for the flexibility of standard Python lists!

Artificial Intelligence
TutorMe
Question:

What is Maximum Likelihood Estimation?

Mayank S.
Answer:

To begin with i’ll take a toy example as follows : Lets take a scenario 1 : I have two groups of randomly generated data : 1.{3,3,3,3,3,5,5,5,5,5,5} 2. {3,3,5,6,7,2,1,4,6,9,11} and I know for sure that these are the only distributions I am concerned with and I need help with finding the parent distribution for the below randomly picked subset : Subset = {3,5} Can you help me in figuring out which of those above distribution is the subset likely to be derived from? Take some time… Yeah,you guessed right(assuming you did!) it seems pretty natural to go with the distribution 1,Yes the intuition is right. But Why? When i asked my friend to answer , he said – “Well since first set has more of 3’s and 5’s ,its more likely that the ‘subset’ belonged to 1 … common sense bro !”. I asked him again if he can show me some mathematical proofs since I am more of a maths oriented person, he further added – “Well a simple probability concept would tell us the reason behind this likelihood – probability of choosing 3 is 5/11 and probability of 5 is 6/11 so the joint probability of getting our ‘subset’ belonging to distribution ‘1’ is 30/121 and for the second it’s 2/121 .So , I went with the first option……Are you happy now?” To read further please refer to my blog at - https://12max34.wordpress.com/2017/04/18/understanding-maximum-likelihood-estimation-the-intuitive-way/

Send a message explaining your
needs and Mayank will reply soon.
Contact Mayank
Ready now? Request a lesson.
Start Session
FAQs
What is a lesson?
A lesson is virtual lesson space on our platform where you and a tutor can communicate. You'll have the option to communicate using video/audio as well as text chat. You can also upload documents, edit papers in real time and use our cutting-edge virtual whiteboard.
How do I begin a lesson?
If the tutor is currently online, you can click the "Start Session" button above. If they are offline, you can always send them a message to schedule a lesson.
Who are TutorMe tutors?
Many of our tutors are current college students or recent graduates of top-tier universities like MIT, Harvard and USC. TutorMe has thousands of top-quality tutors available to work with you.