Python Worst Practices

Daniel Greenfeld
Daniel GreenfeldSoftware Developer at Cartwheel LLC
Python Worst Practices
       Daniel Greenfeld
Daniel Greenfeld
                                        @pydanny




I do cartwheels
        Daniel Greenfeld (@pydanny)
        Pythonista at Cartwheel
        Djangonaut at Revsys
        Co-lead of Django Packages &
           Open Comparison
        Learned Python at NASA
        Fiancé of Audrey Roy
Daniel Greenfeld
                                                     @pydanny




          Talk Format
 Each section will have three components:


• At least one ‘Python Worst Practice’ slide
• At least one ‘Fixed Python Practice’ slide
• A side-by-side comparison slide
        These slides are already online!
Daniel Greenfeld
                                              @pydanny




       Warning!

       Don’t use the
‘Python Worst Practices’
   examples in your code*

You may be hunted down and killed

       *Sometimes these are caught by
         various code checking tools
Daniel Greenfeld
                                   @pydanny




      Advice!

    Do consider using the
‘Fixed Python Practices’
    examples in your code


  You may get complimented
Fundamentals
Daniel Greenfeld
                                                      @pydanny




    Python Worst Practice
object = MyObject()
map = Map()
zip = 90213 # common US developer mistake
id = 34 # I still fight this one

I’m guilty

    • I still use ‘id’ when I shouldn’t
    • I admit I have a problem
    • That gives me license to pick on others
Daniel Greenfeld
                                                                @pydanny




      Fixed Python Practice
obj = MyObject() # necessary abbreviation
object_ = MyObject() # Underscore so we don't overwrite

map_obj = Map() # combine name w/necessary abbreviation
map_ = Map()

zip_code = 90213 # Explicit name with US focus
postal_code = 90213 # i18n explicit name
zip_ = 90213

pk = 34 # pk is often synonymous with id
id_ = 34
Daniel Greenfeld
                                                                                                      @pydanny




   Side-by-side comparison
 Worst Practice                               Fixed Practice
object = MyObject()                         obj = MyObject() # Use a necessary abbreviation
map = Map()                                 object_ = MyObject() # Use underscore so we don't overwrite
zip = 90213 # common US developer mistake
id = 34 # I still fight this one            map_obj = Map() # combine name with necessary abbreviation
                                            map_ = Map()

                                            zip_code = 90213 # Explicit name with US focus
                                            postal_code = 90213 # International explicit name
                                            zip_ = 90213

                                            pk = 34 # pk is often synonymous with id
                                            id_ = 34
Daniel Greenfeld
                                          @pydanny




Python Worst Practice
       Flipping the booleans

          true = 0
          false = 1
          True = False

   Usually done to support an API
Daniel Greenfeld
                                       @pydanny




   This sort of API
def crazy_posting_api(value):
    """ If a value is supplied
     successfully return ‘0’.
        Otherwise return ‘1’
    """
    if value:
        return 0
    return 1
Daniel Greenfeld
                                                              @pydanny




Fixed Python Practice
class CrazyApiConsumer(object):

    def __init__(self, value):
        self.value = value

    def post(self):
        # fix booleans in/around the return statement
        response = crazy_posting_api(self.value)
        return not bool(response)

cac1 = CrazyApiConsumer("hello")
print(cac1.post())
cac2 = CrazyApiConsumer("")
print(cac2.post())
Daniel Greenfeld
                                                                      @pydanny




Side-by-side comparison
Worst Practice   Fixed Practice
true = 0         class CrazyApiConsumer(object):
false = 1
True = False         def __init__(self, value):
                         self.value = value

                     def post(self):
                         # fix booleans in/around the return statement
                         response = crazy_posting_api(self.value)
                         return not bool(response)

                 cac1 = CrazyApiConsumer("hello")
                 print(cac1.post())
                 cac2 = CrazyApiConsumer("")
                 print(cac2.post())
Daniel Greenfeld
                                                  @pydanny




Python Worst Practice
  Identifying variable types with prefixes


 strColor = "green"
 boolActive = False
 intPythonYears = 20
 dtPythonFirstUsed = "04/20/2011"


   Mixing case doesn’t help either
Daniel Greenfeld
                                                 @pydanny




Python Worst Practice
Conserving pixels by removing the vowels


clr = "green"
ctv = False
pythnYrs = 20
pthnFrstSd = "04/20/2011"
Daniel Greenfeld
                                 @pydanny




Python Worst Practice

    c   =   "green"
    a   =   False
    p   =   20
    t   =   "04/20/2011"
Daniel Greenfeld
                                                    @pydanny




Fixed Python Practice
 color = "green"
 active = False
 python_years = 20
 python_first_used = "04/20/2011"

Python assumes we are all consenting adults

 • Infer from naming schemes the type/purpose
 • Don’t be constrained by type
Daniel Greenfeld
                                             @pydanny




Fixed Python Practice
color = "green"
active = False
python_years = 20
python_first_used = "04/20/2011"


     The pixel shortage is over.
 Use reasonably long variable names.
Daniel Greenfeld
                                                                     @pydanny




     Side-by-side comparison
 Worst Practice                    Fixed Practice
 c   =   "green"
 a   =   False                     color = "green"
 p   =   20
 t   =   "04/20/2011"
                                   active = False
                                   python_years = 20
                                   python_first_used = "04/20/2011"
 clr = "green"
 ctv = False
 pythnYrs = 20
 pthnFrstSd = "04/20/2011"




strColor = "green"
boolActive = False
intPythonYears = 20
dtPythonFirstUsed = "04/20/2011"
Daniel Greenfeld
                                             @pydanny




   Python Worst Practice
           Don’t use enumerate


foo = [1, 2, 3]

for i, item in zip(range(len(foo)), foo):
    print i, item
Daniel Greenfeld
                                            @pydanny




Fixed Python Practice
         Use enumerate

foo = [1, 2, 3]

for i, item in enumerate(foo):
    print i, item

   • Memorize the Python built-ins
   • Makes your code easier to read
   • Proven code
Daniel Greenfeld
                                                                    @pydanny




Side-by-side comparison
Worst Practice               Fixed Practice
foo = [1, 2, 3]              foo = [1, 2, 3]

zip(range(len(foo)), foo):   for i, item in enumerate(foo):
    print i, item                print i, item
Python Worst Practice
       Present using


      Different Fonts


        Dark Text


     Dire Backgr#nds
Daniel Greenfeld
                                @pydanny




Python Worst Practice

       Present using

       High Contrast
     Easy-to-read fonts
       All devices off
      All programs off
Daniel Greenfeld
                                              @pydanny




Side-by-side comparison
Worst Practice     Fixed Practice

   Present using
                     Present using
  Different Fonts

                     High Contrast
    Dark Text
                   Easy-to-read fonts
 Dire Backgr#nds     All devices off
                    All programs off
Classes
Daniel Greenfeld
                                                               @pydanny




Python Worst Practice
Implementing Java-style getters and setters
    import logging
    log = logging.getLogger()

    class JavaStyle:
        """ Quiz: what else am I doing wrong here? """

        def __init__(self):
            self.name = ""

        def get_name(self):
            return self.name

        def set_name(self, name):
            log.debug("Setting the name to %s" % name)
            if isinstance(name, str):
                self.name = name
            else:
                raise TypeError()

    if __name__ == "__main__":
        j = JavaStyle()
        j.set_name("pydanny did this back in 2006!")
        print(j.get_name())
Daniel Greenfeld
                                                                                @pydanny




Fixed Python Practice
                Python properties!
 import logging
 log = logging.getLogger()

 class PythonStyle(object):

     def __init__(self):
         self._name = ""                 Accessor
     @property
     def name(self):
         return self._name
                                                 Mutator
     @name.setter
     def name(self, value):
         """ Because name is probably a string we'll assume that we can
                  infer the type from the variable name"""
         log.debug("Setting the name to %s" % value)
         self._name = value

 if __name__ == "__main__":
     p = PythonStyle()
     p.name = "pydanny doing it the right way"
     print(p.name)
Daniel Greenfeld
                                                                                                 @pydanny




      Side-by-side comparison
    Worst Practice                               Fixed Practice
import logging              import logging
log = logging.getLogger()   log = logging.getLogger()

class JavaStyle:            class PythonStyle(object):
    """ Quiz: what else am I doing wrong here? """
                                def __init__(self):
    def __init__(self):             self._name = ""
        self.name = ""
                                @property
    def get_name(self):         def name(self):
        return self.name            return self._name

    def set_name(self, name):   @name.setter
        log.debug("Setting the name to %s" % name)
                                def name(self, value):
        if isinstance(name, str):   """ Because name is probably a string we'll assume that we can
            self.name = name                 infer the type from the variable name"""
        else:                       log.debug("Setting the name to %s" % value)
            raise TypeError()       self._name = value

if __name__ == "__main__": if __name__ == "__main__":
    j = JavaStyle()             p = PythonStyle()
    j.set_name("pydanny did thisp.namein 2006!") doing it the right way"
                                 back = "pydanny
    print(j.get_name())         print(p.name)
Daniel Greenfeld
                                                                         @pydanny




  Python Worst Practice
    Using property setters as action methods!
class WebService(object):

    @property
    def connect(self):
        self.proxy = xmlrpc.Server("http://service.xml")

if __name__ == '__main__':

    ws = WebService()
    ws.connect


            A.K.A.Trying to make your Python code look like Ruby
Daniel Greenfeld
                                                                 @pydanny




  Fixed Python Practice
                  Methods please!
class WebService(object):

    def connect(self):
        self.proxy = xmlrpc.Server("http://service.xml")

if __name__ == '__main__':

    ws = WebService()
    ws.connect()
Daniel Greenfeld
                                                                                               @pydanny




    Side-by-side comparison
  Worst Practice                               Fixed Practice

class WebService(object):         class WebService(object):
    @property
    def connect(self):                      def connect(self):
        self.proxy = xmlrpc.Server("http://service.xml")
                                                  self.proxy = xmlrpc.Server("http://service.xml")
if __name__ == '__main__':
                                  if __name__ == '__main__':
    ws = WebService()
    ws.connect
                                       ws = WebService()
                                       ws.connect()
Exceptions
Daniel Greenfeld
                                                   @pydanny




Python Worst Practice
    Passing Generic Exceptions silently
         try:
             do_akshun(value)
         except:
             pass

• Ignorance is not bliss
• You have no idea what your system is doing
• Arguably better to not have this in your code
Daniel Greenfeld
                                                                  @pydanny




   Fixed Python Practice
       Use specific exceptions and/or logging

class AkshunDoesNotDo(Exception):
    """ Custom exceptions makes for maintainable code """
    pass

try:
    do_akshun(value)
except AttributeError as e:
    log.info("Can I get attribution for these slides?")
    do_bakup_akshun(vlue)
except Exception as e:
    log.debug(str(e))
    raise AkshunDoesNotDo(e)
Daniel Greenfeld
                                                                            @pydanny




Side-by-side comparison
Worst Practice                   Fixed Practice

try:                   class AkshunDoesNotDo(Exception):
    do_akshun(value)       """ Custom exceptions makes for maintainable code """
except:                    pass
    pass
                       try:
                           do_akshun(value)
                       except AttributeError as e:
                           log.info("Can I get attribution for these slides?")
                           do_bakup_akshun(vlue)
                       except Exception as e:
                           log.debug(str(e))
                           raise AkshunDoesNotDo(e)
Getting controversial
Daniel Greenfeld
                                                                @pydanny




    Python Worst Practice
           Using exec for dynamic imports

imports = "from {0} import {1}".format("random", "randrange")
exec(imports)
print(randrange(10))



    • Hard to debug
    • Security risk
    • Sets bad precedents
Daniel Greenfeld
                                                        @pydanny




Fixed Python Practice
    Using importlib for dynamic imports

   import importlib
   funstuff = importlib.import_module('random')
   print(funstuff.randrange(10))



• importlib is in the standard library
• Really explicit
• Direct tie into the Python machinery
Daniel Greenfeld
                                                                                                              @pydanny




       Side-by-side comparison
     Worst Practice                                     Fixed Practice
imports = "from {0} import {1}".format("random", "randrange")
                                                         import importlib
exec(imports)                                            funstuff = importlib.import_module('random')
print(randrange(10))                                     print(funstuff.randrange(10))
Daniel Greenfeld
                                                                                                           @pydanny




       Python Worst Practice
                            Generally using lambdas
swap = lambda a, x, y: lambda f = a.__setitem__: (f(x, (a[x], a[y])), f(y, a[x][0]),   f(x, a[x][1]))()




    • Too many characters on one line
    • Lambdas by design does not have docstrings
    • Does not necessarily mean less characters
    • I can’t get this sample to work!
Daniel Greenfeld
                                                             @pydanny




    Fixed Python Practice
      def swap(a, x, y):
          """ Swap two position values in a list """
          a[x],a[y] = a[y],a[x]



• Doc strings that show up nicely in help/Sphinx
• Easier to read
• In Python, functions are first class objects
• Whenever possible avoid using lambdas
Daniel Greenfeld
                                                                                                @pydanny




 Side-by-side comparison
Worst Practice                           Fixed Practice
swap = lambda a, x, y:                   def swap(a, x, y):
       lambda f = a.__setitem__:             """ Swap two position values in a list """
       (f(x, (a[x], a[y])),                  a[x],a[y] = a[y],a[x]
       f(y, a[x][0]), f(x, a[x][1]))()
Daniel Greenfeld
                                                              @pydanny




Python Worst Practice
      Configuring your project with XML

<pydanny-ml>
    <do action="call_view">com.pydanny.nextSlide</do>
    <global name="spam" value="eggs" />
</pydanny-ml>




•   You can’t convince me that XML is the better way

•   You are forcing me to learn a new language
Daniel Greenfeld
                                                    @pydanny




Fixed Python Practice ?
     Use Python for configuration!
 spam = "eggs"
 actions = [
     ('call_view', 'com.pydanny.nextSlide')
 ]


  • Is this the right way?
   • This allows conditional logic
   • Iterators
   • i.e. “Magic Configuration”
Daniel Greenfeld
                                                                             @pydanny




Python Worst Practice
         ‘Magical configuration code’
 INSTALLED_APPS += [p for p in os.listdir(BASE) if os.path.isdir(p)]
 MIDDLEWARE_CLASSES = [...]
 def callback(arg, dirname, fnames):
     if 'middleware.py' in fnames:
         m = '%s.middleware' % os.path.split(dirname)[-1]
         MIDDLEWARE_CLASSES.append(m)

 urlpatterns = patterns('', ...)
 for app in settings.INSTALLED_APPS:
     if not app.startswith('django'):
         p = url('^%s/' % app, include('%s.urls') % app)
         urlpatterns += patterns('', p)




                              Ugh.
  http://www.slideshare.net/jacobian/the-best-and-worst-of-django
Daniel Greenfeld
                                                                                                @pydanny




          Fixed Python Practice
                                   urlpatterns = patterns("",
PREREQ_APPS = [
    # Django                            url(r"^$", homepage, name="home"),
    "django.contrib.admin",             url(r"^accounts/", include("accounts.urls")),
    "django.contrib.auth",              url(r"^admin/", include(admin.site.urls)),
    "django.contrib.contenttypes",      url(r"^about/", include("about.urls")),
    "django.contrib.sessions",          url(r"^profiles/", include("profiles.urls")),
    "django.contrib.sites",             url(r"^notices/", include("notification.urls")),
    "django.contrib.messages",          ...
    "django.contrib.humanize",  MIDDLEWARE_CLASSES = [
                                        )
    "django.contrib.flatpages",     "django.middleware.common.CommonMiddleware",
                                    "django.contrib.sessions.middleware.SessionMiddleware",
    # external                      "django.middleware.csrf.CsrfViewMiddleware",
    "notification", # must be first "django.contrib.auth.middleware.AuthenticationMiddleware",
    "staticfiles",                  "reversion.middleware.RevisionMiddleware",
    "uni_form",                     "django.contrib.messages.middleware.MessageMiddleware",
    ...                             ...
    ]                               ]


Explicit is better
                                        This isn’t that much typing, is it?
  then Implicit
Daniel Greenfeld
                                                                                                @pydanny




          Fixed Python Practice
                                   urlpatterns = patterns("",
PREREQ_APPS = [
    # Django                            url(r"^$", homepage, name="home"),
    "django.contrib.admin",             url(r"^accounts/", include("accounts.urls")),
    "django.contrib.auth",              url(r"^admin/", include(admin.site.urls)),
    "django.contrib.contenttypes",      url(r"^about/", include("about.urls")),
    "django.contrib.sessions",          url(r"^profiles/", include("profiles.urls")),
    "django.contrib.sites",             url(r"^notices/", include("notification.urls")),
    "django.contrib.messages",          ...
    "django.contrib.humanize",  MIDDLEWARE_CLASSES = [
                                        )
    "django.contrib.flatpages",     "django.middleware.common.CommonMiddleware",
                                    "django.contrib.sessions.middleware.SessionMiddleware",
    # external                      "django.middleware.csrf.CsrfViewMiddleware",
    "notification", # must be first "django.contrib.auth.middleware.AuthenticationMiddleware",
    "staticfiles",                  "reversion.middleware.RevisionMiddleware",
    "uni_form",                     "django.contrib.messages.middleware.MessageMiddleware",
    ...                             ...
    ]                               ]



 Python’s design is predicated on the proposition that
    code is more often read than written.
              http://www.slideshare.net/jacobian/the-best-and-worst-of-django/44
Daniel Greenfeld
                                                        @pydanny




 Fixed Python Practice
               Use a config file

spam = "eggs"

[actions]
call_view = com.pydanny.nextSlide



           Read up on config parser
http://docs.python.org/library/configparser.html
Daniel Greenfeld
                                                @pydanny




Side-by-side comparison
Worst Practice       Fixed Practice

XML                  simple python files
logic heavy python   config (.cfg) files
Documentation
Daniel Greenfeld
                                                              @pydanny




 Python Worst Practice
                   Bad docstrings
class Pythonista(): # Old style class!
    """ This class represents a Python programmer """

     def code(self):
         """Write some code """
         code, inspiration = Code(), Inspiration()
         for hour in Effort():
             try:
                  code += hour + inspiraion
             except CurseWorthyBug:
                  ...


 •   Do really obvious objects require doc strings?

 •   Complex methods require more than docstrings!
Daniel Greenfeld
                                                              @pydanny




      Fixed Python Practice
        class Pythonista(object):

            def code(self):
                """ Writes code following these steps
                1. Create a space for coding
                2. Get some inspiration
                3. Loop through some hours of effort
   Spend a      4. Write some code
few minutes     5. Pull out hair cause of bugs
                """
documenting     code = Code()
 the critical   inspiration = Inspiration()
                for hour in Effort():
 stuff, okay?       try:
                         code += hour + inspiraion
                    except CurseWorthyBug:
                         ...
Daniel Greenfeld
                                                                                        @pydanny




      Side-by-side comparison
     Worst Practice                         Fixed Practice
class Pythonista(): # Old style class!       class Pythonista(object):
    """ This class represents a Python programmer """
                                                 def code(self):
    def code(self):                                  """ Writes code following these steps
        """Write some code """                       1. Create a space for coding
        code, inspiration = Code(), Inspiration()    2. Get some inspiration
        for hour in Effort():                        3. Loop through some hours of effort
            try:                                     4. Write some code
                 code += hour + inspiraion           5. Pull out hair cause of bugs
            except CurseWorthyBug:                   """
              ...                                    code = Code()
                                                     inspiration = Inspiration()
                                                     for hour in Effort():
                                                         try:
                                                              code += hour + inspiraion
                                                         except CurseWorthyBug:
                                                              ...
Daniel Greenfeld
                                                              @pydanny




   Python Worst Practice
      Using a wiki for project documentation

 “Wikis are where project documentation goes to die”
                                         Jacob Kaplan-Moss



• Generally not version controlled
• Backups? Mirrors?
• Editing via the web? Ugh.
• No pull requests - smaller group of contributors
Daniel Greenfeld
                                         @pydanny




Fixed Python Practice

• Use Restructured Text
• Use Sphinx
• Host on http://readthedocs.org
Daniel Greenfeld
                                        @pydanny




Side-by-side comparison
Worst Practice   Fixed Practice
Daniel Greenfeld
                                                                                      @pydanny




Python Worst Practice
   >>> import that
   The Anti-Zen of Python, by Daniel Greenfeld

   Ugly is better than beautiful.
   Implicit is better than explicit.
   Complicated is better than complex.
   Complex is better than simple.
   Nested is better than flat.
   Dense is better than sparse.
   Line code counts.
   Special cases are special enough to break the rules.
   Although purity beats practicality.
   Errors should always pass silently.
   Spelchek iz fur loosers.
   In the face of explicity, succumb to the temptation to guess.
   There should be many ways to do it.
   Because only a tiny minority of us are Dutch.
   Later is the best time to fix something.
   If the implementation is hard to explain, it's a good sell.
   If the implementation is easy to explain, it won't take enough time to do.
   Namespaces are too hard, just use import *!


  http://pypi.python.org/pypi/that
Daniel Greenfeld
                                                                                 @pydanny




Fixed Python Practice
   >>> import this
   The Zen of Python, by Tim Peters

   Beautiful is better than ugly.
   Explicit is better than implicit.
   Simple is better than complex.
   Complex is better than complicated.
   Flat is better than nested.
   Sparse is better than dense.
   Readability counts.
   Special cases aren't special enough to break the rules.
   Although practicality beats purity.
   Errors should never pass silently.
   Unless explicitly silenced.
   In the face of ambiguity, refuse the temptation to guess.
   There should be one-- and preferably only one --obvious way to do it.
   Although that way may not be obvious at first unless you're Dutch.
   Now is better than never.
   Although never is often better than *right* now.
   If the implementation is hard to explain, it's a bad idea.
   If the implementation is easy to explain, it may be a good idea.
   Namespaces are one honking great idea -- let's do more of those!

                Core Python
Daniel Greenfeld
                                                                                                                                  @pydanny




          Side-by-side comparison
        Worst Practice                                             Fixed Practice
                                                         >>> import this
>>> import that
                                                         The Zen of Python, by Tim Peters
The Anti-Zen of Python, by Daniel Greenfeld
                                                            Beautiful is better than ugly.
Ugly is better than beautiful.
                                                            Explicit is better than implicit.
Implicit is better than explicit.
                                                            Simple is better than complex.
Complicated is better than complex.
                                                            Complex is better than complicated.
Complex is better than simple.
                                                            Flat is better than nested.
Nested is better than flat.
                                                            Sparse is better than dense.
Dense is better than sparse.
                                                            Readability counts.
Line code counts.
                                                            Special cases aren't special enough to break the rules.
Special cases are special enough to break the rules.
                                                            Although practicality beats purity.
Although purity beats practicality.
                                                            Errors should never pass silently.
Errors should always pass silently.
                                                            Unless explicitly silenced.
Spelchek iz fur loosers.
                                                            In the face of ambiguity, refuse the temptation to guess.
In the face of explicity, succumb to the temptation to guess.
                                                            There should be one-- and preferably only one --obvious way to do it.
There should be many ways to do it.
                                                            Although that way may not be obvious at first unless you're Dutch.
Because only a tiny minority of us are Dutch.
                                                            Now is better than never.
Later is the best time to fix something.
                                                            Although never is often better than *right* now.
If the implementation is hard to explain, it's a good sell.
                                                            If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it won't take enough time to do.
                                                            If the implementation is easy to explain, it may be a good idea.
Namespaces are too hard, just use import *!
                                                            Namespaces are one honking great idea -- let's do more of those!
Questions?
1 of 61

Recommended

Complexity Analysis of Recursive Function by
Complexity Analysis of Recursive FunctionComplexity Analysis of Recursive Function
Complexity Analysis of Recursive FunctionMeghaj Mallick
181 views19 slides
AFLGo: Directed Greybox Fuzzing by
AFLGo: Directed Greybox FuzzingAFLGo: Directed Greybox Fuzzing
AFLGo: Directed Greybox Fuzzingmboehme
1.6K views57 slides
[MGDC] 리눅스 게임 서버 성능 분석하기 - 아이펀팩토리 김진욱 CTO by
[MGDC] 리눅스 게임 서버 성능 분석하기 - 아이펀팩토리 김진욱 CTO[MGDC] 리눅스 게임 서버 성능 분석하기 - 아이펀팩토리 김진욱 CTO
[MGDC] 리눅스 게임 서버 성능 분석하기 - 아이펀팩토리 김진욱 CTOiFunFactory Inc.
1.6K views74 slides
External to DA, the OS X Way by
External to DA, the OS X WayExternal to DA, the OS X Way
External to DA, the OS X WayStephan Borosh
6.8K views44 slides
Hibernate by
HibernateHibernate
HibernatePrashant Kalkar
844 views74 slides
Python Class | Python Programming | Python Tutorial | Edureka by
Python Class | Python Programming | Python Tutorial | EdurekaPython Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaEdureka!
997 views8 slides

More Related Content

What's hot

Oscp preparation by
Oscp preparationOscp preparation
Oscp preparationManich Koomsusi
4.8K views39 slides
PythonOOP by
PythonOOPPythonOOP
PythonOOPVeera Pendyala
907 views157 slides
Clean Code: Chapter 3 Function by
Clean Code: Chapter 3 FunctionClean Code: Chapter 3 Function
Clean Code: Chapter 3 FunctionKent Huang
5.4K views25 slides
Dynamic pgmming by
Dynamic pgmmingDynamic pgmming
Dynamic pgmmingDr. C.V. Suresh Babu
6.2K views51 slides
PSConfEU - Offensive Active Directory (With PowerShell!) by
PSConfEU - Offensive Active Directory (With PowerShell!)PSConfEU - Offensive Active Directory (With PowerShell!)
PSConfEU - Offensive Active Directory (With PowerShell!)Will Schroeder
9K views16 slides
파이썬 데이터과학 1일차 - 초보자를 위한 데이터분석, 데이터시각화 (이태영) by
파이썬 데이터과학 1일차 - 초보자를 위한 데이터분석, 데이터시각화 (이태영)파이썬 데이터과학 1일차 - 초보자를 위한 데이터분석, 데이터시각화 (이태영)
파이썬 데이터과학 1일차 - 초보자를 위한 데이터분석, 데이터시각화 (이태영)Tae Young Lee
22.9K views126 slides

What's hot(20)

Clean Code: Chapter 3 Function by Kent Huang
Clean Code: Chapter 3 FunctionClean Code: Chapter 3 Function
Clean Code: Chapter 3 Function
Kent Huang5.4K views
PSConfEU - Offensive Active Directory (With PowerShell!) by Will Schroeder
PSConfEU - Offensive Active Directory (With PowerShell!)PSConfEU - Offensive Active Directory (With PowerShell!)
PSConfEU - Offensive Active Directory (With PowerShell!)
Will Schroeder9K views
파이썬 데이터과학 1일차 - 초보자를 위한 데이터분석, 데이터시각화 (이태영) by Tae Young Lee
파이썬 데이터과학 1일차 - 초보자를 위한 데이터분석, 데이터시각화 (이태영)파이썬 데이터과학 1일차 - 초보자를 위한 데이터분석, 데이터시각화 (이태영)
파이썬 데이터과학 1일차 - 초보자를 위한 데이터분석, 데이터시각화 (이태영)
Tae Young Lee22.9K views
Introducing Pair Programming by Steven Smith
Introducing Pair ProgrammingIntroducing Pair Programming
Introducing Pair Programming
Steven Smith4.3K views
git - eine praktische Einführung by Marcel Eichner
git - eine praktische Einführunggit - eine praktische Einführung
git - eine praktische Einführung
Marcel Eichner15.3K views
Version Control History and Git Basics by Sreedath N S
Version Control History and Git BasicsVersion Control History and Git Basics
Version Control History and Git Basics
Sreedath N S939 views
finding Min and max element from given array using divide & conquer by Swati Kulkarni Jaipurkar
finding Min and max element from given array using  divide & conquer finding Min and max element from given array using  divide & conquer
finding Min and max element from given array using divide & conquer
Learn 90% of Python in 90 Minutes by Matt Harrison
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 Minutes
Matt Harrison319.7K views
11. Java Objects and classes by Intro C# Book
11. Java  Objects and classes11. Java  Objects and classes
11. Java Objects and classes
Intro C# Book5.8K views
What is Multithreading In Python | Python Multithreading Tutorial | Edureka by Edureka!
What is Multithreading In Python | Python Multithreading Tutorial | EdurekaWhat is Multithreading In Python | Python Multithreading Tutorial | Edureka
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
Edureka!2.1K views
Introduction to Approximation Algorithms by Jhoirene Clemente
Introduction to Approximation AlgorithmsIntroduction to Approximation Algorithms
Introduction to Approximation Algorithms
Jhoirene Clemente5.4K views
Social Engineering the Windows Kernel by James Forshaw by Shakacon
Social Engineering the Windows Kernel by James ForshawSocial Engineering the Windows Kernel by James Forshaw
Social Engineering the Windows Kernel by James Forshaw
Shakacon11.6K views
Git - An Introduction by Behzad Altaf
Git - An IntroductionGit - An Introduction
Git - An Introduction
Behzad Altaf547 views

Viewers also liked

Thinking hard about_python by
Thinking hard about_pythonThinking hard about_python
Thinking hard about_pythonDaniel Greenfeld
8.5K views120 slides
Python Tricks That You Can't Live Without by
Python Tricks That You Can't Live WithoutPython Tricks That You Can't Live Without
Python Tricks That You Can't Live WithoutAudrey Roy
29.6K views45 slides
Introduction to Python by
Introduction to PythonIntroduction to Python
Introduction to PythonNowell Strite
167.9K views64 slides
Python 101: Python for Absolute Beginners (PyTexas 2014) by
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Paige Bailey
29.8K views52 slides
Introduction to Python by
Introduction to Python Introduction to Python
Introduction to Python amiable_indian
36.8K views62 slides
Object-oriented Programming in Python by
Object-oriented Programming in PythonObject-oriented Programming in Python
Object-oriented Programming in PythonJuan-Manuel Gimeno
7.2K views233 slides

Viewers also liked(20)

Python Tricks That You Can't Live Without by Audrey Roy
Python Tricks That You Can't Live WithoutPython Tricks That You Can't Live Without
Python Tricks That You Can't Live Without
Audrey Roy29.6K views
Introduction to Python by Nowell Strite
Introduction to PythonIntroduction to Python
Introduction to Python
Nowell Strite167.9K views
Python 101: Python for Absolute Beginners (PyTexas 2014) by Paige Bailey
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)
Paige Bailey29.8K views
Introduction to Python by amiable_indian
Introduction to Python Introduction to Python
Introduction to Python
amiable_indian36.8K views
Introduction to python for Beginners by Sujith Kumar
Introduction to python for Beginners Introduction to python for Beginners
Introduction to python for Beginners
Sujith Kumar129.5K views
Advanced Python : Static and Class Methods by Bhanwar Singh Meena
Advanced Python : Static and Class Methods Advanced Python : Static and Class Methods
Advanced Python : Static and Class Methods
Bhanwar Singh Meena21.7K views
Advance OOP concepts in Python by Sujith Kumar
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
Sujith Kumar19.3K views
Python Programming Language by Laxman Puri
Python Programming LanguagePython Programming Language
Python Programming Language
Laxman Puri15.6K views
Python PPT by Edureka!
Python PPTPython PPT
Python PPT
Edureka!38.9K views
Django Package Thunderdome by Audrey Roy & Daniel Greenfeld by Audrey Roy
Django Package Thunderdome by Audrey Roy & Daniel GreenfeldDjango Package Thunderdome by Audrey Roy & Daniel Greenfeld
Django Package Thunderdome by Audrey Roy & Daniel Greenfeld
Audrey Roy18.4K views
Basics of Object Oriented Programming in Python by Sujith Kumar
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
Sujith Kumar35.4K views
Python Advanced – Building on the foundation by Kevlin Henney
Python Advanced – Building on the foundationPython Advanced – Building on the foundation
Python Advanced – Building on the foundation
Kevlin Henney154K views

More from Daniel Greenfeld

How to Write a Popular Python Library by Accident by
How to Write a Popular Python Library by AccidentHow to Write a Popular Python Library by Accident
How to Write a Popular Python Library by AccidentDaniel Greenfeld
1.9K views114 slides
10 more-things-you-can-do-with-python by
10 more-things-you-can-do-with-python10 more-things-you-can-do-with-python
10 more-things-you-can-do-with-pythonDaniel Greenfeld
3.1K views20 slides
From NASA to Startups to Big Commerce by
From NASA to Startups to Big CommerceFrom NASA to Startups to Big Commerce
From NASA to Startups to Big CommerceDaniel Greenfeld
93.2K views208 slides
Intro to Data Visualizations by
Intro to Data VisualizationsIntro to Data Visualizations
Intro to Data VisualizationsDaniel Greenfeld
3K views59 slides
An Extreme Talk about the Zen of Python by
An Extreme Talk about the Zen of PythonAn Extreme Talk about the Zen of Python
An Extreme Talk about the Zen of PythonDaniel Greenfeld
3.9K views101 slides
PyCon Philippines 2012 Keynote by
PyCon Philippines 2012 KeynotePyCon Philippines 2012 Keynote
PyCon Philippines 2012 KeynoteDaniel Greenfeld
1.4K views158 slides

More from Daniel Greenfeld(20)

How to Write a Popular Python Library by Accident by Daniel Greenfeld
How to Write a Popular Python Library by AccidentHow to Write a Popular Python Library by Accident
How to Write a Popular Python Library by Accident
Daniel Greenfeld1.9K views
10 more-things-you-can-do-with-python by Daniel Greenfeld
10 more-things-you-can-do-with-python10 more-things-you-can-do-with-python
10 more-things-you-can-do-with-python
Daniel Greenfeld3.1K views
From NASA to Startups to Big Commerce by Daniel Greenfeld
From NASA to Startups to Big CommerceFrom NASA to Startups to Big Commerce
From NASA to Startups to Big Commerce
Daniel Greenfeld93.2K views
An Extreme Talk about the Zen of Python by Daniel Greenfeld
An Extreme Talk about the Zen of PythonAn Extreme Talk about the Zen of Python
An Extreme Talk about the Zen of Python
Daniel Greenfeld3.9K views
Lighting talk on django-social-auth by Daniel Greenfeld
Lighting talk on django-social-authLighting talk on django-social-auth
Lighting talk on django-social-auth
Daniel Greenfeld4.6K views

Recently uploaded

PharoJS - Zürich Smalltalk Group Meetup November 2023 by
PharoJS - Zürich Smalltalk Group Meetup November 2023PharoJS - Zürich Smalltalk Group Meetup November 2023
PharoJS - Zürich Smalltalk Group Meetup November 2023Noury Bouraqadi
126 views17 slides
virtual reality.pptx by
virtual reality.pptxvirtual reality.pptx
virtual reality.pptxG036GaikwadSnehal
11 views15 slides
Spesifikasi Lengkap ASUS Vivobook Go 14 by
Spesifikasi Lengkap ASUS Vivobook Go 14Spesifikasi Lengkap ASUS Vivobook Go 14
Spesifikasi Lengkap ASUS Vivobook Go 14Dot Semarang
37 views1 slide
The details of description: Techniques, tips, and tangents on alternative tex... by
The details of description: Techniques, tips, and tangents on alternative tex...The details of description: Techniques, tips, and tangents on alternative tex...
The details of description: Techniques, tips, and tangents on alternative tex...BookNet Canada
126 views24 slides
Perth MeetUp November 2023 by
Perth MeetUp November 2023 Perth MeetUp November 2023
Perth MeetUp November 2023 Michael Price
19 views44 slides
AMAZON PRODUCT RESEARCH.pdf by
AMAZON PRODUCT RESEARCH.pdfAMAZON PRODUCT RESEARCH.pdf
AMAZON PRODUCT RESEARCH.pdfJerikkLaureta
19 views13 slides

Recently uploaded(20)

PharoJS - Zürich Smalltalk Group Meetup November 2023 by Noury Bouraqadi
PharoJS - Zürich Smalltalk Group Meetup November 2023PharoJS - Zürich Smalltalk Group Meetup November 2023
PharoJS - Zürich Smalltalk Group Meetup November 2023
Noury Bouraqadi126 views
Spesifikasi Lengkap ASUS Vivobook Go 14 by Dot Semarang
Spesifikasi Lengkap ASUS Vivobook Go 14Spesifikasi Lengkap ASUS Vivobook Go 14
Spesifikasi Lengkap ASUS Vivobook Go 14
Dot Semarang37 views
The details of description: Techniques, tips, and tangents on alternative tex... by BookNet Canada
The details of description: Techniques, tips, and tangents on alternative tex...The details of description: Techniques, tips, and tangents on alternative tex...
The details of description: Techniques, tips, and tangents on alternative tex...
BookNet Canada126 views
Perth MeetUp November 2023 by Michael Price
Perth MeetUp November 2023 Perth MeetUp November 2023
Perth MeetUp November 2023
Michael Price19 views
AMAZON PRODUCT RESEARCH.pdf by JerikkLaureta
AMAZON PRODUCT RESEARCH.pdfAMAZON PRODUCT RESEARCH.pdf
AMAZON PRODUCT RESEARCH.pdf
JerikkLaureta19 views
Lilypad @ Labweek, Istanbul, 2023.pdf by Ally339821
Lilypad @ Labweek, Istanbul, 2023.pdfLilypad @ Labweek, Istanbul, 2023.pdf
Lilypad @ Labweek, Istanbul, 2023.pdf
Ally3398219 views
Piloting & Scaling Successfully With Microsoft Viva by Richard Harbridge
Piloting & Scaling Successfully With Microsoft VivaPiloting & Scaling Successfully With Microsoft Viva
Piloting & Scaling Successfully With Microsoft Viva
Igniting Next Level Productivity with AI-Infused Data Integration Workflows by Safe Software
Igniting Next Level Productivity with AI-Infused Data Integration Workflows Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Safe Software257 views
Attacking IoT Devices from a Web Perspective - Linux Day by Simone Onofri
Attacking IoT Devices from a Web Perspective - Linux Day Attacking IoT Devices from a Web Perspective - Linux Day
Attacking IoT Devices from a Web Perspective - Linux Day
Simone Onofri15 views
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive by Network Automation Forum
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLiveAutomating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Voice Logger - Telephony Integration Solution at Aegis by Nirmal Sharma
Voice Logger - Telephony Integration Solution at AegisVoice Logger - Telephony Integration Solution at Aegis
Voice Logger - Telephony Integration Solution at Aegis
Nirmal Sharma31 views
Transcript: The Details of Description Techniques tips and tangents on altern... by BookNet Canada
Transcript: The Details of Description Techniques tips and tangents on altern...Transcript: The Details of Description Techniques tips and tangents on altern...
Transcript: The Details of Description Techniques tips and tangents on altern...
BookNet Canada135 views
1st parposal presentation.pptx by i238212
1st parposal presentation.pptx1st parposal presentation.pptx
1st parposal presentation.pptx
i2382129 views

Python Worst Practices

  • 1. Python Worst Practices Daniel Greenfeld
  • 2. Daniel Greenfeld @pydanny I do cartwheels Daniel Greenfeld (@pydanny) Pythonista at Cartwheel Djangonaut at Revsys Co-lead of Django Packages & Open Comparison Learned Python at NASA Fiancé of Audrey Roy
  • 3. Daniel Greenfeld @pydanny Talk Format Each section will have three components: • At least one ‘Python Worst Practice’ slide • At least one ‘Fixed Python Practice’ slide • A side-by-side comparison slide These slides are already online!
  • 4. Daniel Greenfeld @pydanny Warning! Don’t use the ‘Python Worst Practices’ examples in your code* You may be hunted down and killed *Sometimes these are caught by various code checking tools
  • 5. Daniel Greenfeld @pydanny Advice! Do consider using the ‘Fixed Python Practices’ examples in your code You may get complimented
  • 7. Daniel Greenfeld @pydanny Python Worst Practice object = MyObject() map = Map() zip = 90213 # common US developer mistake id = 34 # I still fight this one I’m guilty • I still use ‘id’ when I shouldn’t • I admit I have a problem • That gives me license to pick on others
  • 8. Daniel Greenfeld @pydanny Fixed Python Practice obj = MyObject() # necessary abbreviation object_ = MyObject() # Underscore so we don't overwrite map_obj = Map() # combine name w/necessary abbreviation map_ = Map() zip_code = 90213 # Explicit name with US focus postal_code = 90213 # i18n explicit name zip_ = 90213 pk = 34 # pk is often synonymous with id id_ = 34
  • 9. Daniel Greenfeld @pydanny Side-by-side comparison Worst Practice Fixed Practice object = MyObject() obj = MyObject() # Use a necessary abbreviation map = Map() object_ = MyObject() # Use underscore so we don't overwrite zip = 90213 # common US developer mistake id = 34 # I still fight this one map_obj = Map() # combine name with necessary abbreviation map_ = Map() zip_code = 90213 # Explicit name with US focus postal_code = 90213 # International explicit name zip_ = 90213 pk = 34 # pk is often synonymous with id id_ = 34
  • 10. Daniel Greenfeld @pydanny Python Worst Practice Flipping the booleans true = 0 false = 1 True = False Usually done to support an API
  • 11. Daniel Greenfeld @pydanny This sort of API def crazy_posting_api(value): """ If a value is supplied successfully return ‘0’. Otherwise return ‘1’ """ if value: return 0 return 1
  • 12. Daniel Greenfeld @pydanny Fixed Python Practice class CrazyApiConsumer(object): def __init__(self, value): self.value = value def post(self): # fix booleans in/around the return statement response = crazy_posting_api(self.value) return not bool(response) cac1 = CrazyApiConsumer("hello") print(cac1.post()) cac2 = CrazyApiConsumer("") print(cac2.post())
  • 13. Daniel Greenfeld @pydanny Side-by-side comparison Worst Practice Fixed Practice true = 0 class CrazyApiConsumer(object): false = 1 True = False def __init__(self, value): self.value = value def post(self): # fix booleans in/around the return statement response = crazy_posting_api(self.value) return not bool(response) cac1 = CrazyApiConsumer("hello") print(cac1.post()) cac2 = CrazyApiConsumer("") print(cac2.post())
  • 14. Daniel Greenfeld @pydanny Python Worst Practice Identifying variable types with prefixes strColor = "green" boolActive = False intPythonYears = 20 dtPythonFirstUsed = "04/20/2011" Mixing case doesn’t help either
  • 15. Daniel Greenfeld @pydanny Python Worst Practice Conserving pixels by removing the vowels clr = "green" ctv = False pythnYrs = 20 pthnFrstSd = "04/20/2011"
  • 16. Daniel Greenfeld @pydanny Python Worst Practice c = "green" a = False p = 20 t = "04/20/2011"
  • 17. Daniel Greenfeld @pydanny Fixed Python Practice color = "green" active = False python_years = 20 python_first_used = "04/20/2011" Python assumes we are all consenting adults • Infer from naming schemes the type/purpose • Don’t be constrained by type
  • 18. Daniel Greenfeld @pydanny Fixed Python Practice color = "green" active = False python_years = 20 python_first_used = "04/20/2011" The pixel shortage is over. Use reasonably long variable names.
  • 19. Daniel Greenfeld @pydanny Side-by-side comparison Worst Practice Fixed Practice c = "green" a = False color = "green" p = 20 t = "04/20/2011" active = False python_years = 20 python_first_used = "04/20/2011" clr = "green" ctv = False pythnYrs = 20 pthnFrstSd = "04/20/2011" strColor = "green" boolActive = False intPythonYears = 20 dtPythonFirstUsed = "04/20/2011"
  • 20. Daniel Greenfeld @pydanny Python Worst Practice Don’t use enumerate foo = [1, 2, 3] for i, item in zip(range(len(foo)), foo): print i, item
  • 21. Daniel Greenfeld @pydanny Fixed Python Practice Use enumerate foo = [1, 2, 3] for i, item in enumerate(foo): print i, item • Memorize the Python built-ins • Makes your code easier to read • Proven code
  • 22. Daniel Greenfeld @pydanny Side-by-side comparison Worst Practice Fixed Practice foo = [1, 2, 3] foo = [1, 2, 3] zip(range(len(foo)), foo): for i, item in enumerate(foo): print i, item print i, item
  • 23. Python Worst Practice Present using Different Fonts Dark Text Dire Backgr#nds
  • 24. Daniel Greenfeld @pydanny Python Worst Practice Present using High Contrast Easy-to-read fonts All devices off All programs off
  • 25. Daniel Greenfeld @pydanny Side-by-side comparison Worst Practice Fixed Practice Present using Present using Different Fonts High Contrast Dark Text Easy-to-read fonts Dire Backgr#nds All devices off All programs off
  • 27. Daniel Greenfeld @pydanny Python Worst Practice Implementing Java-style getters and setters import logging log = logging.getLogger() class JavaStyle: """ Quiz: what else am I doing wrong here? """ def __init__(self): self.name = "" def get_name(self): return self.name def set_name(self, name): log.debug("Setting the name to %s" % name) if isinstance(name, str): self.name = name else: raise TypeError() if __name__ == "__main__": j = JavaStyle() j.set_name("pydanny did this back in 2006!") print(j.get_name())
  • 28. Daniel Greenfeld @pydanny Fixed Python Practice Python properties! import logging log = logging.getLogger() class PythonStyle(object): def __init__(self): self._name = "" Accessor @property def name(self): return self._name Mutator @name.setter def name(self, value): """ Because name is probably a string we'll assume that we can infer the type from the variable name""" log.debug("Setting the name to %s" % value) self._name = value if __name__ == "__main__": p = PythonStyle() p.name = "pydanny doing it the right way" print(p.name)
  • 29. Daniel Greenfeld @pydanny Side-by-side comparison Worst Practice Fixed Practice import logging import logging log = logging.getLogger() log = logging.getLogger() class JavaStyle: class PythonStyle(object): """ Quiz: what else am I doing wrong here? """ def __init__(self): def __init__(self): self._name = "" self.name = "" @property def get_name(self): def name(self): return self.name return self._name def set_name(self, name): @name.setter log.debug("Setting the name to %s" % name) def name(self, value): if isinstance(name, str): """ Because name is probably a string we'll assume that we can self.name = name infer the type from the variable name""" else: log.debug("Setting the name to %s" % value) raise TypeError() self._name = value if __name__ == "__main__": if __name__ == "__main__": j = JavaStyle() p = PythonStyle() j.set_name("pydanny did thisp.namein 2006!") doing it the right way" back = "pydanny print(j.get_name()) print(p.name)
  • 30. Daniel Greenfeld @pydanny Python Worst Practice Using property setters as action methods! class WebService(object): @property def connect(self): self.proxy = xmlrpc.Server("http://service.xml") if __name__ == '__main__': ws = WebService() ws.connect A.K.A.Trying to make your Python code look like Ruby
  • 31. Daniel Greenfeld @pydanny Fixed Python Practice Methods please! class WebService(object): def connect(self): self.proxy = xmlrpc.Server("http://service.xml") if __name__ == '__main__': ws = WebService() ws.connect()
  • 32. Daniel Greenfeld @pydanny Side-by-side comparison Worst Practice Fixed Practice class WebService(object): class WebService(object): @property def connect(self): def connect(self): self.proxy = xmlrpc.Server("http://service.xml") self.proxy = xmlrpc.Server("http://service.xml") if __name__ == '__main__': if __name__ == '__main__': ws = WebService() ws.connect ws = WebService() ws.connect()
  • 34. Daniel Greenfeld @pydanny Python Worst Practice Passing Generic Exceptions silently try: do_akshun(value) except: pass • Ignorance is not bliss • You have no idea what your system is doing • Arguably better to not have this in your code
  • 35. Daniel Greenfeld @pydanny Fixed Python Practice Use specific exceptions and/or logging class AkshunDoesNotDo(Exception): """ Custom exceptions makes for maintainable code """ pass try: do_akshun(value) except AttributeError as e: log.info("Can I get attribution for these slides?") do_bakup_akshun(vlue) except Exception as e: log.debug(str(e)) raise AkshunDoesNotDo(e)
  • 36. Daniel Greenfeld @pydanny Side-by-side comparison Worst Practice Fixed Practice try: class AkshunDoesNotDo(Exception): do_akshun(value) """ Custom exceptions makes for maintainable code """ except: pass pass try: do_akshun(value) except AttributeError as e: log.info("Can I get attribution for these slides?") do_bakup_akshun(vlue) except Exception as e: log.debug(str(e)) raise AkshunDoesNotDo(e)
  • 38. Daniel Greenfeld @pydanny Python Worst Practice Using exec for dynamic imports imports = "from {0} import {1}".format("random", "randrange") exec(imports) print(randrange(10)) • Hard to debug • Security risk • Sets bad precedents
  • 39. Daniel Greenfeld @pydanny Fixed Python Practice Using importlib for dynamic imports import importlib funstuff = importlib.import_module('random') print(funstuff.randrange(10)) • importlib is in the standard library • Really explicit • Direct tie into the Python machinery
  • 40. Daniel Greenfeld @pydanny Side-by-side comparison Worst Practice Fixed Practice imports = "from {0} import {1}".format("random", "randrange") import importlib exec(imports) funstuff = importlib.import_module('random') print(randrange(10)) print(funstuff.randrange(10))
  • 41. Daniel Greenfeld @pydanny Python Worst Practice Generally using lambdas swap = lambda a, x, y: lambda f = a.__setitem__: (f(x, (a[x], a[y])), f(y, a[x][0]), f(x, a[x][1]))() • Too many characters on one line • Lambdas by design does not have docstrings • Does not necessarily mean less characters • I can’t get this sample to work!
  • 42. Daniel Greenfeld @pydanny Fixed Python Practice def swap(a, x, y): """ Swap two position values in a list """ a[x],a[y] = a[y],a[x] • Doc strings that show up nicely in help/Sphinx • Easier to read • In Python, functions are first class objects • Whenever possible avoid using lambdas
  • 43. Daniel Greenfeld @pydanny Side-by-side comparison Worst Practice Fixed Practice swap = lambda a, x, y: def swap(a, x, y): lambda f = a.__setitem__: """ Swap two position values in a list """ (f(x, (a[x], a[y])), a[x],a[y] = a[y],a[x] f(y, a[x][0]), f(x, a[x][1]))()
  • 44. Daniel Greenfeld @pydanny Python Worst Practice Configuring your project with XML <pydanny-ml> <do action="call_view">com.pydanny.nextSlide</do> <global name="spam" value="eggs" /> </pydanny-ml> • You can’t convince me that XML is the better way • You are forcing me to learn a new language
  • 45. Daniel Greenfeld @pydanny Fixed Python Practice ? Use Python for configuration! spam = "eggs" actions = [ ('call_view', 'com.pydanny.nextSlide') ] • Is this the right way? • This allows conditional logic • Iterators • i.e. “Magic Configuration”
  • 46. Daniel Greenfeld @pydanny Python Worst Practice ‘Magical configuration code’ INSTALLED_APPS += [p for p in os.listdir(BASE) if os.path.isdir(p)] MIDDLEWARE_CLASSES = [...] def callback(arg, dirname, fnames): if 'middleware.py' in fnames: m = '%s.middleware' % os.path.split(dirname)[-1] MIDDLEWARE_CLASSES.append(m) urlpatterns = patterns('', ...) for app in settings.INSTALLED_APPS: if not app.startswith('django'): p = url('^%s/' % app, include('%s.urls') % app) urlpatterns += patterns('', p) Ugh. http://www.slideshare.net/jacobian/the-best-and-worst-of-django
  • 47. Daniel Greenfeld @pydanny Fixed Python Practice urlpatterns = patterns("", PREREQ_APPS = [ # Django url(r"^$", homepage, name="home"), "django.contrib.admin", url(r"^accounts/", include("accounts.urls")), "django.contrib.auth", url(r"^admin/", include(admin.site.urls)), "django.contrib.contenttypes", url(r"^about/", include("about.urls")), "django.contrib.sessions", url(r"^profiles/", include("profiles.urls")), "django.contrib.sites", url(r"^notices/", include("notification.urls")), "django.contrib.messages", ... "django.contrib.humanize", MIDDLEWARE_CLASSES = [ ) "django.contrib.flatpages", "django.middleware.common.CommonMiddleware", "django.contrib.sessions.middleware.SessionMiddleware", # external "django.middleware.csrf.CsrfViewMiddleware", "notification", # must be first "django.contrib.auth.middleware.AuthenticationMiddleware", "staticfiles", "reversion.middleware.RevisionMiddleware", "uni_form", "django.contrib.messages.middleware.MessageMiddleware", ... ... ] ] Explicit is better This isn’t that much typing, is it? then Implicit
  • 48. Daniel Greenfeld @pydanny Fixed Python Practice urlpatterns = patterns("", PREREQ_APPS = [ # Django url(r"^$", homepage, name="home"), "django.contrib.admin", url(r"^accounts/", include("accounts.urls")), "django.contrib.auth", url(r"^admin/", include(admin.site.urls)), "django.contrib.contenttypes", url(r"^about/", include("about.urls")), "django.contrib.sessions", url(r"^profiles/", include("profiles.urls")), "django.contrib.sites", url(r"^notices/", include("notification.urls")), "django.contrib.messages", ... "django.contrib.humanize", MIDDLEWARE_CLASSES = [ ) "django.contrib.flatpages", "django.middleware.common.CommonMiddleware", "django.contrib.sessions.middleware.SessionMiddleware", # external "django.middleware.csrf.CsrfViewMiddleware", "notification", # must be first "django.contrib.auth.middleware.AuthenticationMiddleware", "staticfiles", "reversion.middleware.RevisionMiddleware", "uni_form", "django.contrib.messages.middleware.MessageMiddleware", ... ... ] ] Python’s design is predicated on the proposition that code is more often read than written. http://www.slideshare.net/jacobian/the-best-and-worst-of-django/44
  • 49. Daniel Greenfeld @pydanny Fixed Python Practice Use a config file spam = "eggs" [actions] call_view = com.pydanny.nextSlide Read up on config parser http://docs.python.org/library/configparser.html
  • 50. Daniel Greenfeld @pydanny Side-by-side comparison Worst Practice Fixed Practice XML simple python files logic heavy python config (.cfg) files
  • 52. Daniel Greenfeld @pydanny Python Worst Practice Bad docstrings class Pythonista(): # Old style class! """ This class represents a Python programmer """ def code(self): """Write some code """ code, inspiration = Code(), Inspiration() for hour in Effort(): try: code += hour + inspiraion except CurseWorthyBug: ... • Do really obvious objects require doc strings? • Complex methods require more than docstrings!
  • 53. Daniel Greenfeld @pydanny Fixed Python Practice class Pythonista(object): def code(self): """ Writes code following these steps 1. Create a space for coding 2. Get some inspiration 3. Loop through some hours of effort Spend a 4. Write some code few minutes 5. Pull out hair cause of bugs """ documenting code = Code() the critical inspiration = Inspiration() for hour in Effort(): stuff, okay? try: code += hour + inspiraion except CurseWorthyBug: ...
  • 54. Daniel Greenfeld @pydanny Side-by-side comparison Worst Practice Fixed Practice class Pythonista(): # Old style class! class Pythonista(object): """ This class represents a Python programmer """ def code(self): def code(self): """ Writes code following these steps """Write some code """ 1. Create a space for coding code, inspiration = Code(), Inspiration() 2. Get some inspiration for hour in Effort(): 3. Loop through some hours of effort try: 4. Write some code code += hour + inspiraion 5. Pull out hair cause of bugs except CurseWorthyBug: """ ... code = Code() inspiration = Inspiration() for hour in Effort(): try: code += hour + inspiraion except CurseWorthyBug: ...
  • 55. Daniel Greenfeld @pydanny Python Worst Practice Using a wiki for project documentation “Wikis are where project documentation goes to die” Jacob Kaplan-Moss • Generally not version controlled • Backups? Mirrors? • Editing via the web? Ugh. • No pull requests - smaller group of contributors
  • 56. Daniel Greenfeld @pydanny Fixed Python Practice • Use Restructured Text • Use Sphinx • Host on http://readthedocs.org
  • 57. Daniel Greenfeld @pydanny Side-by-side comparison Worst Practice Fixed Practice
  • 58. Daniel Greenfeld @pydanny Python Worst Practice >>> import that The Anti-Zen of Python, by Daniel Greenfeld Ugly is better than beautiful. Implicit is better than explicit. Complicated is better than complex. Complex is better than simple. Nested is better than flat. Dense is better than sparse. Line code counts. Special cases are special enough to break the rules. Although purity beats practicality. Errors should always pass silently. Spelchek iz fur loosers. In the face of explicity, succumb to the temptation to guess. There should be many ways to do it. Because only a tiny minority of us are Dutch. Later is the best time to fix something. If the implementation is hard to explain, it's a good sell. If the implementation is easy to explain, it won't take enough time to do. Namespaces are too hard, just use import *! http://pypi.python.org/pypi/that
  • 59. Daniel Greenfeld @pydanny Fixed Python Practice >>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those! Core Python
  • 60. Daniel Greenfeld @pydanny Side-by-side comparison Worst Practice Fixed Practice >>> import this >>> import that The Zen of Python, by Tim Peters The Anti-Zen of Python, by Daniel Greenfeld Beautiful is better than ugly. Ugly is better than beautiful. Explicit is better than implicit. Implicit is better than explicit. Simple is better than complex. Complicated is better than complex. Complex is better than complicated. Complex is better than simple. Flat is better than nested. Nested is better than flat. Sparse is better than dense. Dense is better than sparse. Readability counts. Line code counts. Special cases aren't special enough to break the rules. Special cases are special enough to break the rules. Although practicality beats purity. Although purity beats practicality. Errors should never pass silently. Errors should always pass silently. Unless explicitly silenced. Spelchek iz fur loosers. In the face of ambiguity, refuse the temptation to guess. In the face of explicity, succumb to the temptation to guess. There should be one-- and preferably only one --obvious way to do it. There should be many ways to do it. Although that way may not be obvious at first unless you're Dutch. Because only a tiny minority of us are Dutch. Now is better than never. Later is the best time to fix something. Although never is often better than *right* now. If the implementation is hard to explain, it's a good sell. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it won't take enough time to do. If the implementation is easy to explain, it may be a good idea. Namespaces are too hard, just use import *! Namespaces are one honking great idea -- let's do more of those!