Let's Learn Python #18 - Factory and @classmethod

preview_player
Показать описание
This week, I cover what a factory is, how to create one, and the decorator Classmethod.

Please leave me a comment or question below! Like and Subscribe to show your support! :D

=========================================

Music by Juto: Link Coming Soon!

=========================================
--- SUPER TUTORIAL LIST!!! ---

PLAYLISTS

=========================================
Рекомендации по теме
Комментарии
Автор

For classmethods you use class for the first parameter, not an instance. Therefore, by convention you need to pass cls, not self. self is used for instances, cls for classes

harrisonnhill
Автор

Man, thanks for all your tutorial. I had to learn python for a project in engineering, and you saved my butt.

MexterO
Автор

Thank you so much! I have been struggling in computing keeping up with python but your explanations are really clear and concise. Please keep these coming!

Sagaofsr
Автор

That Harry Potter cartoon at 4:53 is so cool! Also, great tutorials!

ON-nerd
Автор

I will definitely look into it! I won't be able to get to it for a few months, have a lot more lined up for you guys! Thanks for the suggestion! :D

Anchor_Rainbow
Автор

Thanks a lot, by the way, it will be amazing if you make a second part of this series with python 3.5 or more, pleaaaase. Thaanks !

rodrigocastilla
Автор

coding w/ Sans serif, mix of PascalCase w/ camelCase.. this guy

chuckynorris
Автор

excellent explanation! very clear! thanks thanks.

arunprasath
Автор

Ohw yes i did these Challenges exactly the same way as you explained in the Solutions :D ( except i had eggs instead of pancakes for C3 lol)
I took some days of rest before watching this video to let the past lessons sink in and it worked :)
Thanks again for these awesome video's. I'm starting to get the hang of it.

Sjoerdjoetoep
Автор

awesum man...

do some more tutorials in python...

bjugdbjk
Автор

Hi Payne,
Great video indeed, Could you teach us about monkey patching and name mangling concepts in Python..?

sijojose
Автор

While defining a factory method, can you please explain why base class is needed. I am able to achieve the same property using object as base class

skalangi
Автор

What kind of information that a function is able to access while a class cannot?

Ami-vedette
Автор

Regarding @classmethod. Coming from a java background, it looks like a static method. However, seeing it defined outside of the class definition block is awkward to me. Is this the same as:

class C1(BaseClass):

  x = 1

  def Check1(self, myStr):
    return myStr == "ham"

  check = Check1


Also, I see that in your classmethod and factory functions you capitalize the first letter, like is common with classes... is that standard in python?

kevincfunk
Автор

Instead of class method, the below also works, how is this different from @classmethod functionality?
class Sample:
def func():
print("Sample function")

Sample.func()

rajarajeshwaripremkumar
Автор

Hi, i wrote your code and i am getting this error:

TypeError: type() argument 2 must be tuple, not type

I am using Python 3.4

trezesp
Автор

pep8... please... this is not really pythonic ^

itayel
Автор

Why not have the factory build the class you want?  For example, this will get you the same result, but without these unnecessary check functions and class declarations:  

BaseClass = type("BaseClass", (object, ), {})

ids = {"ham": 1, "sandwich": 30}

def MyFactory(myStr):
    return type("Class_%s", (BaseClass, ), {"x" : ids[myStr]})

m = MyFactory("ham")
v = MyFactory("sandwich")

print m.x, v.x

JeremyVoltzMusic
Автор

I don't get the second half of this tutorial at all. Nothing prevents you from calling a class method without creating an instance of it ordinarily, you just have to pass an object to take the place of the self variable. In this case because printHam doesn't actually do anything to the self variable you can pass it any object with any value and it will work just fine.

AthensHorseParty
Автор

this tutorial isn't working for me. all you do is make different ways to print ham and you do your best to shorten everything up as much as possible. that might work for you but for me it isn't working at all. i need to write a lot of stuff and when it becomes boring and repetitive you say write some more of that and then when it's really unbearable you show a shorter way to write the same thing. that's how i would learn because at that point i would want to know the easier way. what you're doing in these videos isn't motivating me to do anything because i'm not learning anything. this is basically a showoff of your understanding of certain concepts of python which would impress a teacher in school or someone like that but it does nothing for me and i assume you made these videos for people like me and not for school.

eotikurac